title: Flutter How to refresh StreamBuilder?
body: <p>Consider the following code:</p>
  
  <pre><code>StreamBuilder&lt;QuerySnapshot&gt; _createDataStream(){   
      return StreamBuilder&lt;QuerySnapshot&gt;(
            stream: Firestore.instance.collection("data").limit.(_myLimit).snapshots(),
            builder: (context, snapshot){
                return Text(_myLimit.toString);   
              }
          );
  }
  </code></pre>
  
  <p>I want that the StreamBuilder refreshes when the <code>_myLimit</code> Variable changes.
  It's possible doing it like this:</p>
  
  <pre><code>void _incrementLimit(){
      setState(() =&gt;_myLimit++);
  }
  </code></pre>
  
  <p>My Question is if there is another, faster way, except the <code>setState((){});</code> one.
  Because I don't want to recall the whole <code>build()</code> method when the <code>_myLimit</code> Variable changes.</p>
  
  <p>I figured out another Way but I feel like there is a even better solution because I think I don't make use of the <code>.periodic</code> functionality and I got a nested Stream I'm not sure how usual this is:</p>
  
  <pre><code>Stream&lt;int&gt; myStream = Stream.periodic(Duration(), (_) =&gt; _myLimit);
  ...
  @override
  Widget build(BuildContext context){
  ...
  return StreamBuilder&lt;int&gt;(
                      stream: myStream,
                      builder: (context, snapshot){
                        return _createDataStream;
                      },
                    ),
  ...
  }
  </code></pre>
  
  <hr>
  
  <p><strong>Solution(s)</strong></p>
  
  <pre><code>import 'dart:async';
  import 'package:flutter/material.dart';
  
  void main() =&gt; runApp(new MyApp());
  
  class MyApp extends StatefulWidget {
    @override
    State&lt;StatefulWidget&gt; createState() {
      return new _MyAppState();
    }
  }
  
  class _MyAppState extends State&lt;MyApp&gt; {
  
    int myNum = 0;
  
    final StreamController _myStreamCtrl = StreamController.broadcast();
    Stream get onVariableChanged =&gt; _myStreamCtrl.stream;
    void updateMyUI() =&gt; _myStreamCtrl.sink.add(myNum);
  
    @override
    void initState() {
      super.initState();
    }
  
    @override
    void dispose() {
      _myStreamCtrl.close();
      super.dispose();
    }
  
    @override
    Widget build(BuildContext context) {
      return MaterialApp(
        home: Scaffold(
          body: Center(
            child:
            Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: &lt;Widget&gt;[
              StreamBuilder(
                stream: onVariableChanged,
                builder: (context, snapshot){
                  if(snapshot.connectionState == ConnectionState.waiting){
                    updateMyUI();
                    return Text(". . .");
                  }
                  return Text(snapshot.data.toString());
                },
              ),
              RaisedButton(
                child: Text("Increment"),
                onPressed: (){
                  myNum++;
                  updateMyUI();
                  },
          )
          ],
        ),
      )));
    }
  }
  
  </code></pre>
  
  <p>Some other ideas, how the StreamBuilder also could look like:</p>
  
  <pre><code>StreamBuilder(
    stream: onVariableChanged,
    builder: (context, snapshot){
      if(snapshot.connectionState == ConnectionState.waiting){
        return Text(myNum.toString());
      }
      return Text(snapshot.data.toString());
    },
  ),
  </code></pre>
  
  <pre><code>StreamBuilder(
    stream: onVariableChanged,
    initialData: myNum,
    builder: (BuildContext context, AsyncSnapshot snapshot){
      if(snapshot.data == null){
        return Text("...");
      }
      return Text(snapshot.data.toString());
    },
  ),
  </code></pre>