【问题标题】:Flutter does popping a screen with a firestore stream reread the documentFlutter 确实会弹出一个带有 Firestore 流的屏幕,重新读取文档
【发布时间】:2021-04-20 21:22:29
【问题描述】:

我对flutter和firebase相当陌生,我目前正在开发一个应用程序,它有一个带有来自firestore快照的流的streambuilder,statefulwidget类或多或少是这样的:

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

我像这样从 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());
  }
}

问题是,当我使用该流构建器关闭屏幕然后重新打开它时,它会再次从 Firestore 读取数据(并使我的读取计数加倍)吗?如果是,那我怎么可能避免重读?

谢谢,任何建议都将不胜感激

【问题讨论】:

    标签: firebase flutter google-cloud-firestore


    【解决方案1】:

    可能没有。您可以检查此更改:

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

    【讨论】:

    • 感谢您的回答,我已经尝试过了,它说它来自缓存,但奇怪的是,假设我在列表中有 7 项,而我只编辑 1 项,不应该从来自firestore的缓存和1?因为当我打印时,它说所有这些都来自 Firestore,而不是来自缓存。它应该是这样的吗?谢谢你
    猜你喜欢
    • 1970-01-01
    • 2020-12-11
    • 1970-01-01
    • 2020-02-24
    • 2020-07-31
    • 2021-09-28
    • 2021-07-25
    • 2021-06-02
    • 2021-03-22
    相关资源
    最近更新 更多