首页 > 解决方案 > 当flutter没有连接时如何从缓存中获取firestore查询?

问题描述

在我的主页中,每次用户打开应用程序时,我都需要获取当前用户数据。但我想从缓存中获取这些数据。根据文档,离线持久性默认在 android 和 iOS 中。

UserRef.doc(FirebaseAuth.instance.currentUser.uid).get()
        .then((DocumentSnapshot snapshot) {

      if(snapshot.exists){
        print('this is exists');
        userExistence=true;
        setState(() {

        });
      }

我运行这些代码,initState但它没有从离线缓存中获取

标签: fluttergoogle-cloud-firestore

解决方案


当应用程序未连接/无法连接到数据库服务器时,get()调用将已经从本地缓存返回结果。如果这是您第一次调用 Firestore,这可能需要一些时间,因为在这种情况下,客户端将首先尝试访问服务器 - 并且只有在连接失败时才从缓存中返回结果。

如果您已经知道客户端没有 Internet 连接,则可以get通过指定它必须从缓存中返回结果来加快调用速度:

get(GetOptions(source: Source.cache))

另请参阅DocumentReference.get().


推荐阅读