firebase - 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(),);
}
},
);
}
解决方案
推荐阅读
- amazon-web-services - 如何使用 AWS 为移动设备保护 HLS 流式传输?
- django - 如何在 Django 模板中呈现新的 HTML 内容?
- python - 如何从烂番茄中抓取超过一页的评论评论?
- python - 如何使用 python tkinter 接受用户输入并使用 selenium Web 自动化将其输入到浏览器结帐中?
- ag-grid - 在 ag-grid 中使用树数据时,可编辑列组不更新文本
- javascript - 是否有按名称访问元素的 css 计数器变量的 javascript 简写?
- laravel - FullCalendar - 无法检索 json 数据,并在日历上显示事件
- pandas - pandas_udf 错误(applyInPandas)说明元组格式
- regex - 我一直在尝试将特定路径中的所有 url 重定向为小写
- typescript - 使用重载函数处理联合,该函数在单个重载中不处理整个联合