我正在为我的应用程序创建一个新闻动态,当我向下滚动时,数据从Firestore中获取。但只要我向上滚动,列表视图就会立即跳到顶部,跳过所有其他项目。如果我使用静态数据加载它,则列表视图正常工作,但当我从Firestore拉取数据时,问题重新出现。我不确定是什么原因导致了这种行为。
这是我的代码:
return StreamBuilder(
stream: Firestore.instance.collection(Constants.NEWS_FEED_NODE)
.document("m9yyOjsPxV06BrxUtHdp").
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) {
return FeedItem2(feed: Feed.fireStore(
snapshot: snapshot.data.documents[index]),);
});
},
);
return FeedItem2()
替换为return Container(padding: EdgeInsets.all(50.0), child: Text("test"));
并查看问题是否消失?如果是这样,可能指向FeedItem2
widget 是问题所在,这种情况下我们需要查看该 widget 的代码。我假设该 widget 正在执行某些动态操作。 - Ashton ThomascacheExtent: 10000.0
传递给 ListView 工厂构造函数。这可能有助于我们确定是否存在问题,即当您滚动到当前可见内容时,ListView 如何处理小部件的释放和重新初始化。 - Ashton ThomascacheExtent
对解决这个问题确实有帮助,但它也需要相当大的内存。难道没有其他方法来解决这个问题吗?这似乎是 Flutter 应用程序的一个重大问题。 - Dennis Ashford