Flutter ListView捕捉到顶部

时间:2018-07-24 21:26:33

标签: firebase dart google-cloud-firestore flutter

我正在为我的应用程序制作新闻提要,其中它检索存储在Firestore中的帖子。我可以很好地发布帖子,但是当我向上滚动时会发生问题。例如,假设正在显示项目6和7,而我尝试向上滚动。滚动一遍后,该应用程序会立即捕捉到顶部。这是说明我要描述的视频。提要中的第一篇文章标题为“ Serene”,请注意,当我向下滚动并向上滚动时,应用程序会跳回到“ Serene”。

Video of demonstration

我不确定是什么原因导致了这种行为。这是我的代码:

loadFeed(){
    //first get the locally saved details of the user before calling Firestore
    return FutureBuilder(
      future: getUserLocally(),
      builder: (BuildContext context, AsyncSnapshot snapshot) {
        if(snapshot.hasData) {
          User user = snapshot.data;
          print("+++ ${user.userId}");
          //once the user has been retrieved, we can now use the userId to make a
          //call to Firestore. We first retrieve all the post ids under the user's news feed
          return StreamBuilder(
            stream: Firestore.instance.collection(Constants.NEWS_FEED_NODE)
                .document(user.userId).
            collection(Constants.POSTS_NODE).orderBy("timestamp",descending: true).snapshots(),
            builder: (BuildContext context, AsyncSnapshot snapshot) {
              if (!snapshot.hasData)
                return Center(child: CircularProgressIndicator(),);

              if (snapshot.data.documents.length == 0)
                return Container();
              //after getting the post ids, we then make a call to FireStore again
              //to retrieve the details of the individual posts
              return ListView.builder(
                  itemCount: snapshot.data.documents.length,
                  itemBuilder: (_, int index) {
                    //save the snapshot of the post which has the following details:
                    //postId, userId, timeStamp, and like status
                    DocumentSnapshot shot = snapshot.data.documents[index];
                    return StreamBuilder(
                      stream: Firestore.instance.collection(Constants.USERS_NODE).
                      document(shot["user_id"]).collection(Constants.POSTS_NODE).document(shot.documentID).snapshots(),
                      builder: (BuildContext context, AsyncSnapshot snap) {
                        if(!snap.hasData) return Container();

                        return FeedItem(feed: Feed.fireStore(
                            snapshot: snap.data),userId: user.userId,postLiked: shot["liked"],);
                      },
                    );
                    /*return FeedItem(feed: Feed.fireStore(
                        snapshot: snapshot.data.documents[index]));*/
                  });
            },
          );
        }else{
          return Center(child: CircularProgressIndicator(),);
        }
      },
    );
  }

0 个答案:

没有答案