简体   繁体   English

Flutter 使用 firestore 弹出屏幕 stream 重新阅读文档

[英]Flutter does popping a screen with a firestore stream reread the document

I am fairly new on flutter and firebase, i am currently developing app which have a streambuilder with stream from a firestore snapshot, the statefulwidget class is more or less like this:我是 flutter 和 firebase 上的新手,我目前正在开发一个应用程序,它有一个来自 firestore 快照的带有 stream 的流生成器,statefulwidget class 或多或少是这样的:

StreamBuilder(
 stream: ItemService().getItemsWithStatus([1]),
  builder: (context, AsyncSnapshot<List<Item>> snapshot) {
     //Widget to show information
                                 
     }
  )

and i get the stream from itemService class like this我像这样从 itemService class 得到 stream

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

the question is, when i close the screen with that streambuilder and then reopen it would it read the data again from firestore (and doubling my read count)?问题是,当我用那个 streambuilder 关闭屏幕然后重新打开它时,它会再次从 firestore 读取数据(并将我的读取计数加倍)吗? if yes then how can i possibly do to avoid reread?如果是,那么我该怎么做才能避免重读?

Thankyou, any advice will really be appreciated谢谢,任何建议将不胜感激

Probably no.可能没有。 You can check with this alteration:您可以检查此更改:

(DocumentSnapshot document) {
  print(document.metadata.isFromCache);
  Item.fromDb(document.data());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM