title: Flutter Streambuilder rebuilds constantly
body: <p>I have a stream builder listening to Firestore, and it kinda works. The main issue is that the streambuilder keeps rebuilding (if it doesn't cost me extra reads then no worries, but if it does then it's a problem which I'm assuming it does). When the main page first builds, when I segue to another page it builds again, and when I pop back to the main page it rebuilds for a third time.</p>
  
  <p>Code:</p>
  
  <pre><code>class _RequestsListState extends State&lt;RequestsList&gt; with AutomaticKeepAliveClientMixin {
    final Map&lt;String, List&lt;Post&gt;&gt; posts = {};
    final List&lt;String&gt; postsUserIDs = [];
  
    @override
    bool get wantKeepAlive =&gt; true;
  
    @override
    Widget build(BuildContext context) {
      super.build(context);
  
      print('RequestsList');
      User cUser = InheritedUser.of(context).user;
  
      return StreamBuilder&lt;List&lt;Post&gt;&gt;(
          initialData: cUser.posts,
          stream: APIs().posts.auPostsStream(cUserID: cUser.userID),
          builder: (context, snap) {
            if (snap.hasError) {
              print('AUPostsList.dart StreamBuilder Error: ${snap.error}');
              return null;
            } else {
  
              print('POSTS LENGTH');
              print(snap.connectionState);
              print(cUser.posts.length);
  
              this.posts.clear();
              this.postsUserIDs.clear();
  
              snap.data.forEach((post) {
                if (this.posts[post.user.documentID] == null) {
                  this.posts[post.user.documentID] = [post];
                } else {
                  this.posts[post.user.documentID].add(post);
                }
  
                if (!this.postsUserIDs.contains(post.user.documentID)) {
                  this.postsUserIDs.add(post.user.documentID);
                }
              });
  
              return ListView.separated(
                  controller: widget.scrollController,
                  physics: BouncingScrollPhysics(),
                  shrinkWrap: true,
                  padding: EdgeInsets.only(top: 0.0),
                  itemBuilder: (context, index) =&gt; RequestItem(posts: this.posts[this.postsUserIDs[index]]),
                  separatorBuilder: (context, index) =&gt; Container(),
                  itemCount: this.postsUserIDs.length);
            }
          });
    }
  }
  </code></pre>
  
  <p>And secondly, the when I segue to the second page, the array gets emptied. I'm not clearing it anywhere so I'm not sure why it empties the array after tapping an item...</p>
  
  <p>Log:</p>
  
  <pre><code>Reloaded 0 of 773 
  libraries in 169ms.
  flutter: ChatsList
  flutter: RequestsList
  flutter: POSTS LENGTH
  flutter: ConnectionState.waiting
  flutter: 1
  flutter: RequestsList
  flutter: POSTS LENGTH
  flutter: ConnectionState.waiting
  flutter: 0
  flutter: ChatsList
  flutter: RequestsList
  flutter: POSTS LENGTH
  flutter: ConnectionState.waiting
  flutter: 0
  flutter: ChatsList
  </code></pre>