我有一个导航视图上有多个索引的应用程序,当我第一次加载该应用程序时,正如预期的那样,StreamBuilder 被调用两次。但是,当我导航到另一个索引并使用流构建器返回构建时,似乎正在调用流构建器来重新下载其中的所有项目。我担心如果这个循环运行的时间太长,这会消耗我所有的 firebase 数据。如何重构以阻止 StreamBuilder 在切换页面时被多次调用。请注意,当我切换并返回时,我什至保存了该索引的状态。
我的构建方法:
@override
Widget build(BuildContext context) {
return StreamBuilder<QuerySnapshot>(
stream: Firestore.instance
.collection("posts/player/post")
.orderBy("time", descending: true)
.snapshots()
.take(2),
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
if (!snapshot.hasData) return const Text('Loading...');
final int highLightCount = snapshot.data.documents.length;
return new StreamBuilder<QuerySnapshot>(
stream: Firestore.instance
.collection("posts/player/post")
.snapshots(),
builder: (BuildContext context,
AsyncSnapshot<QuerySnapshot> snapshot) {
if (!snapshot.hasData)
return new Text("There are no current posts");
return new ListView(
physics: const AlwaysScrollableScrollPhysics(),
scrollDirection: Axis.vertical,
shrinkWrap: true,
children: getPostItems(snapshot),
);
});
});
}
Run Code Online (Sandbox Code Playgroud)
当我切换它时,它正在调用几乎不间断地调用的 getPostItem(snaphot) ,我如何才能阻止调用它并消耗我的数据,或者阻止 StreamBuilder …