首页 > 解决方案 > Flutter ListView 吸附到顶部

问题描述

我正在为我的应用程序做一个新闻提要,它检索存储在 Firestore 中的帖子。我可以很好地拉出帖子,但是当我向上滚动时会出现问题。例如,假设正在显示第 6 项和第 7 项,我尝试向上滚动。滚动一次后,该应用程序从字面上捕捉到顶部。这是说明我要描述的内容的视频。提要中的第一个帖子有标题“Serene”,请注意,在我向下和向上滚动后,应用程序会一直跳回“Serene”。

演示视频

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

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(),);
        }
      },
    );
  }

标签: firebasedartgoogle-cloud-firestoreflutter

解决方案


推荐阅读