【问题标题】:can I manually set the data source of Firestore in Flutter to only come from Cache?我可以手动将 Flutter 中 Firestore 的数据源设置为仅来自缓存吗?
【发布时间】:2021-08-01 13:49:25
【问题描述】:

我计划使用 Flutter 重新创建我的原生 Android 和 iOS 应用,因此在深入研究 Flutter 之前,我想确保在 Flutter 上提供我需要的一切。

因此,在原生 Android 和 iOS 中,我可以手动设置我的数据是来自 Firestore 服务器还是来自缓存,使用这样的 Kotlin 代码强制系统仅从缓存中获取数据,而不是从服务器获取数据。

db.document(path).get(Source.CACHE).addOnSuccessListener { 
            
}

我可以使用 FlutterFire 在 Flutter 中获得等效的行为吗?我尝试阅读 documentation in here ,但找不到。

在我看来,FlutterFire 中的文档不如原生 in here 的文档完整。例如,可以像here 中的答案一样使用字段值进行更新,但我在 FlutterFire 文档中找不到。

我错过了什么吗?我是 Flutter 的新手 :)

【问题讨论】:

    标签: firebase flutter google-cloud-firestore


    【解决方案1】:

    是的,这是可能的。我们可以参考pub linkdocument link

    DocumentSnapshot snapshot = await FirebaseFirestore.instance
              .collection("collection_name")
              .doc("document_name")
              .get(GetOptions(source: Source.cache));
    
    /// Reads the document referenced by this [DocumentReference].
    ///
    /// By providing [options], this method can be configured to fetch results only
    /// from the server, only from the local cache or attempt to fetch results
    /// from the server and fall back to the cache (which is the default).
    
    Future<DocumentSnapshot> get([GetOptions options]) async {
        return DocumentSnapshot._(
            firestore, await _delegate.get(options ?? const GetOptions()));
    }
    

    【讨论】:

    • 谢谢 Mardel,但你怎么知道它的存在?我试图打开这两个链接,但我仍然找不到它
    • 我有将 Native(Swift 和 Java)移植到 Flutter 的经验。如果我们无法从他们的链接中找到解决方案,我们可以从 VSCode 或 Android Studio 检查他们的库。
    猜你喜欢
    • 1970-01-01
    • 2015-06-22
    • 2021-05-18
    • 1970-01-01
    • 2021-12-18
    • 1970-01-01
    • 2020-04-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多