title: Preventing Flutter StreamBuilder from Duplicating Data in ListView
body: <p>I'm having an issue with StreamBuilder and ListView. </p>
  
  <p>My initial build works as intended, loading all nodes from my DB and adding them to ListView:</p>
  
  <p>--Node 1
  --Node 2
  --Node 3</p>
  
  <p>However, when a new node is added to the DB (Node 4), the StreamBuilder recognizes the change and appends the entire list of nodes to the ListView, resulting a duplicate data:</p>
  
  <p>--Node 1
  --Node 2
  --Node 3
  <strong>--Node 1
  --Node 2
  --Node 3
  --Node 4</strong></p>
  
  <pre><code>
  class _HomeScreenState extends State&lt;HomeScreen&gt; {
    DatabaseReference usersChatsRef =
        FirebaseDatabase().reference().child('users-chats');
  
    @override
    void initState() {
      super.initState();
  
      Stream&lt;List&lt;UsersChats&gt;&gt; getData(User currentUser) async* {
        var usersChatsStream = usersChatsRef.child(currentUser.uid).onValue;
        var foundChats = List&lt;UsersChats&gt;();
  
        await for (var userChatSnapshot in usersChatsStream) {
          Map dictionary = userChatSnapshot.snapshot.value;
          if (dictionary != null) {
            for (var dictItem in dictionary.entries) {
              UsersChats thisChat;
              if (dictItem.key != null) {
                thisChat = UsersChats.fromMap(dictItem);
              } else {
                thisChat = UsersChats();
              }
              foundChats.add(thisChat);
            }
          }
  
          yield foundChats;
        }
      }
    }
  
    @override
    Widget build(BuildContext context) {
      final user = Provider.of&lt;User&gt;(context);
  
      return Scaffold(
        appBar: AppBar(),
        body: StreamBuilder&lt;List&lt;UsersChats&gt;&gt;(
            stream: getData(user),
            builder:
                (BuildContext context, AsyncSnapshot&lt;List&lt;UsersChats&gt;&gt; snap) {
              if (snap.hasError || !snap.hasData)
                return Text('Error: ${snap.error}');
              switch (snap.connectionState) {
                case ConnectionState.waiting:
                  return Text("Loading...");
                default:
                  return ListView.builder(
                    itemCount: snap.data.length,
                    itemBuilder: (context, index) {
                      return ListTile(
                        title: Text(snap.data[index].id),
                        subtitle: Text(snap.data[index].lastUpdate),
                      );
                    },
                  );
              }
            }),
      );
    }
  }
  </code></pre>
  
  <pre><code>class UsersChats {
    String id;
    String lastUpdate;
  
    UsersChats(
        {this.id,
        this.lastUpdate});
  
    factory UsersChats.fromMap(MapEntry&lt;dynamic, dynamic&gt; data) {
      return UsersChats(
          id: data.key ?? '',
          lastUpdate: data.value['lastUpdate'] ?? '');
    }
  }
  </code></pre>
  
  <p>I'm referencing the stream outside of the build method because I need to perform multiple async functions on the stream (as discussed in this thread <a href="https://stackoverflow.com/questions/59061225/how-do-i-join-data-from-two-firestore-collections-in-flutter/59061226#59061226">How do I join data from two Firestore collections in Flutter?</a>).</p>
  
  <p>Any help would be greatly appreciated!</p>