StreamBuilder - Flutter

Last updated Jul 24, 2020

In This post we are going to learn StreamBuilder in flutter


The Stream builder needs mainly 3 parameters: stream, builder, and initialData. The initial is used as a dummy or empty element to act as the first element.

The builder method receives context and snapshot of the stream.

We can use stream.connectionState to find the status of the stream,

We can use stream.hasData ad to build widget when the widget is build.

We can also use stream.hasError to build when an Error occurs


Let's start

Step 1: Create Flutter Application

Step 2: Create dart file and update below code

import 'dart:async';

import 'package:flutter/material.dart';

class StreamBuilderWidget extends StatelessWidget{
  var index = 0;
  StreamSubscription subscription;

  var streamController = StreamController();

  //To Emit the stream
  StreamSink get streamSink  => streamController.sink;

  Stream get streamData =>;


  void onFloatActionButtonPress() {


  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: AppBar(title: Text("Stream Builder"),
      body: Center(
        child:  StreamBuilder(
            stream: streamData,
            builder: (BuildContext context, AsyncSnapshot snapshot) {
              return Text('Result: ${}');
        floatingActionButton: FloatingActionButton(
            onPressed: onFloatActionButtonPress,
            child: Icon(Icons.add))





Step 3: Update main dart file with below code

void main() => runApp(MyApp());

class MyApp extends StatelessWidget{
  Widget build(BuildContext context) {
    // TODO: implement build
    return MaterialApp(
      theme: ThemeData(
      home: StreamBuilderWidget(),



Step 4: Run application


Article Contributed By :