title: Flutter refresh data in my listview
body: <p><strong>UPDATE</strong>
  This is the StreamBuilder code:
  I am trying currently to update with a timer that runs a Stream.fromFuture which updates the data, but with the flicker and scroll weirdness.</p>
  
  <pre><code>new StreamBuilder(
                  initialData: myInitialData,
                  stream: msgstream,
                  builder: (BuildContext context, AsyncSnapshot&lt;List&lt;Map&gt;&gt; snapshot) {
                    switch (snapshot.connectionState) {
                      case ConnectionState.none:
                        return new Text('Waiting to start');
                      case ConnectionState.waiting:
                        return new Text('');
                      default:
                        if (snapshot.hasError) {
                          return new Text('Error: ${snapshot.error}');
                        } else {
                          myInitialData = snapshot.data;
                          return new RefreshIndicator(
                              child: new ListView.builder(
                                itemBuilder: (context, index) {
                                  Stream&lt;List&lt;Map&gt;&gt; msgstream2;
                                  Future&lt;List&lt;Map&gt;&gt; _responseDate = ChatDB.instance.getMessagesByDate(snapshot.data[index]['msgkey'], snapshot.data[index]['msgdate']);
                                  msgstream2 = new Stream.fromFuture(_responseDate);
                                  return new StreamBuilder(
                                    initialData: myInitialData2,
                                    stream: msgstream2,
                                    builder: (BuildContext context, AsyncSnapshot&lt;List&lt;Map&gt;&gt; snapshot) {
                                      switch (snapshot.connectionState) {
                                        case ConnectionState.none:
                                          return new Text('Waiting to start');
                                        case ConnectionState.waiting:
                                          return new Text('');
                                        default:
                                          List messList;
                                          var mybytes;
                                          File myimageview;
                                          Image newimageview;
                                          String imgStr;
                                          String vidStr;
                                          String vidimgstr;
                                          myInitialData2 = snapshot.data;
                                          List&lt;dynamic&gt; json = snapshot.data;
                                          List messagelist = [];
                                          json.forEach((element) {
  
                                            DateTime submitdate =
                                            DateTime.parse(element['submitdate']).toLocal();
                                            String myvideo = (element['chatvideo']);
                                            String myimage = element['chatimage'];
                                            String myvideoimage = element['chatvideoimage'];
                                            File imgfile;
                                            File vidfile;
                                            File vidimgfile;
                                            bool vidInit = false;
                                            Future&lt;Null&gt; _launched;
                                            String localAssetPath;
                                            String localVideoPath;
                                            String mymessage = element['message'].replaceAll("[\u2018\u2019]", "'");
                                            //print('MYDATE: '+submitdate.toString());
                                            _checkFile(File file) async {
                                              var checkfile = await file.exists();
                                              print('VIDEXISTS: '+checkfile.toString());
                                            }
                                            Future&lt;Null&gt; _launchVideo(String url, bool isLocal) async {
                                              if (await canLaunchVideo(url, isLocal)) {
                                                await launchVideo(url, isLocal);
                                              } else {
                                                throw 'Could not launch $url';
                                              }
                                            }
                                            void _launchLocal() =&gt;
                                                setState(() =&gt; _launched = _launchVideo(localVideoPath, true)
                                                );
  
                                            Widget _showVideo() {
                                              return new Flexible(
                                                  child: new Card(
                                                    child: new Column(
                                                      children: &lt;Widget&gt;[
                                                        new ListTile(subtitle: new Text('Video'), title: new Text(element['referralname']),),
                                                        new GestureDetector(
                                                          onTap: _launchLocal,
                                                          child: new Image.file(
                                                            vidimgfile,
                                                            width: 150.0,
                                                          ),
                                                        ),
                                                      ],
                                                    ),
                                                  )
                                              );
                                            }
  
                                            if (myimage != "") {
                                              imgStr = element['chatimage'];
                                              imgfile = new File(imgStr);
                                            }
                                            if (myvideo != "") {
                                              vidStr = element['chatvideo'];
                                              vidimgstr = element['chatvideoimage'];
                                              vidimgfile = new File(vidimgstr);
  
                                              localVideoPath = '$vidStr';
  
  
                                            }
  
                                            _showLgPic() {
                                              Route route = new MaterialPageRoute(
                                                settings: new RouteSettings(name: "/ShowPic"),
                                                builder: (BuildContext context) =&gt; new ShowPic(
                                                  image: imgfile,
                                                ),
                                              );
                                              Navigator.of(context).push(route);
                                            }
  
                                            Widget _showGraphic() {
                                              Widget mywidget;
                                              if (myimage != "") {
                                                mywidget = new GestureDetector(
                                                  child: new Image.file(
                                                    imgfile,
                                                    width: 300.0,
                                                  ),
                                                  onTap: _showLgPic,
                                                );
                                              } else if (myvideo != "") {
                                                mywidget = _showVideo();
                                              } else {
                                                mywidget = new Container();
                                              }
                                              return mywidget;
                                            }
  
                                            messagelist.add(
                                              new Container(
                                                //width: 300.0,
                                                padding: new EdgeInsets.all(10.0),
                                                child: new Column(
                                                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                                                  crossAxisAlignment: CrossAxisAlignment.stretch,
                                                  mainAxisSize: MainAxisSize.min,
                                                  children: &lt;Widget&gt;[
                                                    new Container(
                                                      padding: new EdgeInsets.only(bottom: 5.0),
                                                      child: new Row(
                                                        mainAxisSize: MainAxisSize.min,
                                                        children: &lt;Widget&gt;[
                                                          new CircleAvatar(
                                                            child: new Text(
                                                              element['sendname'][0],
                                                              style: new TextStyle(fontSize: 15.0),
                                                            ),
                                                            radius: 12.0,
                                                          ),
                                                          new Text('    '),
                                                          new Text(
                                                            element['sendname'],
                                                            style: new TextStyle(
                                                                fontSize: 15.0,
                                                                fontWeight: FontWeight.bold),
                                                          ),
                                                          new Text('    '),
                                                          new Text(
                                                            new DateFormat.Hm().format(submitdate),
                                                            style: new TextStyle(
                                                                color: Colors.grey, fontSize: 12.0),
                                                          ),
  
                                                        ],
                                                      ),
                                                    ),
                                                    new Row(
                                                      children: &lt;Widget&gt;[
                                                        new Text('          '),
                                                        new Flexible(
                                                          child: new Text(mymessage),
                                                        )
                                                      ],
                                                    ),
                                                    new Container(
                                                        width: 150.0,
                                                        child: new Row(
                                                          children: &lt;Widget&gt;[
                                                            new Text('          '),
                                                            _showGraphic()
                                                          ],
                                                        )),
                                                  ],
                                                ),
                                              ),
                                            );
                                          });
                                          return new Column(children: messagelist);
                                      }
                                    }
                                  );
                                  /*return new MyChatWidget(
                                    datediv: snapshot.data[index]['msgdate'],
                                    msgkey: snapshot.data[index]['msgkey'],
                                  );*/
  
                                },
                                //itemBuilder: _itemBuilder,
                                controller: _scrollController,
                                reverse: true,
                                itemCount: snapshot.data.length,
                              ),
                              onRefresh: _onRefresh
                          );
                        }
  
                    }
                  }),
  </code></pre>
  
  <p>I started with a Future> from a local sqlite DB.  I take that future and use the data to to get another Future> from the DB.  I use Listview.builder to build the widget, etc...  All works great, but need to refresh the data in realtime as messages come in and get updated in the DB.  I converted the furtures to streams and use a timer to go get new data, but of course my screen flickers and eventhough the data refreshes its ugly.</p>
  
  <p>So I am trying to figure out a better way to get the data to update without the flicker and not affect the user if they are scrolling on the page looking at messages.</p>
  
  <p>I currently do the 2 futures, because one is used to build Date Dividers between messages for each Date.  I have been looking at have a StreamController and subscribing to it, but not clear how to update the data in the controller as the the data needs to be full when they come to the page and then added to as the new messages sync.</p>
  
  <p>So I have been looking at something like this that I found:</p>
  
  <pre><code>class Server {
    StreamController&lt;List&lt;Map&gt;&gt; _controller = new StreamController.broadcast();
    void addMessage(int message) {
      var newmsg = await database.rawQuery('select c.*, date(submitdate, "localtime") as msgtime from v_groupchats g join chats c on c.id = g.id where (oid = $oid or prid = $prid) and c.msgkey not in (select msgkey from chatArchive) order by submitdate desc');
      _controller.add(message);
    }
    Stream get messages =&gt; _controller.stream;
  }
  </code></pre>
  
  <p>This is not complete, just hoping it helps someone with some ideas for me.</p>
  
  <p>Thanks in advance for any help.</p>