Firebase Flatter使用firestore流弹出屏幕来重新读取文档
我对Flatter和firebase相当陌生,我目前正在开发一个应用程序,该应用程序有一个streambuilder,其中包含来自firestore快照的流,statefulwidget类大致如下:Firebase Flatter使用firestore流弹出屏幕来重新读取文档,firebase,flutter,google-cloud-firestore,Firebase,Flutter,Google Cloud Firestore,我对Flatter和firebase相当陌生,我目前正在开发一个应用程序,该应用程序有一个streambuilder,其中包含来自firestore快照的流,statefulwidget类大致如下: StreamBuilder( stream: ItemService().getItemsWithStatus([1]), builder: (context, AsyncSnapshot<List<Item>> snapshot) { //Widget to
StreamBuilder(
stream: ItemService().getItemsWithStatus([1]),
builder: (context, AsyncSnapshot<List<Item>> snapshot) {
//Widget to show information
}
)
StreamBuilder(
流:ItemService().getItemsWithStatus([1]),
生成器:(上下文,异步快照){
//显示信息的小部件
}
)
我从itemService类中得到流,如下所示
class ItemService{
Stream<List<Item>> getItemsWithStatus(List<int> status) {
return _firestore
.collection(itemsPath)
.where('status', whereIn: status)
.snapshots()
.map((QuerySnapshot snapshot) => snapshot.docs
.map((DocumentSnapshot document) => Item.fromDb(document.data()))
.toList());
}
}
class项目服务{
流getItemsWithStatus(列表状态){
返回火场
.collection(itemsPath)
.其中(“状态”,其中:状态)
.快照()
.map((QuerySnapshot快照)=>snapshot.docs
.map((DocumentSnapshot document)=>Item.fromDb(document.data())
.toList());
}
}
问题是,当我用streambuilder关闭屏幕,然后重新打开时,它是否会再次从firestore读取数据(并将读取计数加倍)?如果是,我该如何避免重读
谢谢,如果您有任何建议,我们将不胜感激。可能没有。您可以查看此更改:
(DocumentSnapshot document) {
print(document.metadata.isFromCache);
Item.fromDb(document.data());
}
谢谢你的回答,我已经试过了,上面说是从缓存中得到的,但奇怪的是,假设我在列表中有7个项目,我只编辑了1个项目,那么它不是应该从缓存中得到6个,从firestore中得到1个吗?因为当我打印出来的时候,上面说的都是来自firestore而不是缓存。应该是这样吗?谢谢你