flutter - 当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
但它没有从离线缓存中获取
解决方案
当应用程序未连接/无法连接到数据库服务器时,get()
调用将已经从本地缓存返回结果。如果这是您第一次调用 Firestore,这可能需要一些时间,因为在这种情况下,客户端将首先尝试访问服务器 - 并且只有在连接失败时才从缓存中返回结果。
如果您已经知道客户端没有 Internet 连接,则可以get
通过指定它必须从缓存中返回结果来加快调用速度:
get(GetOptions(source: Source.cache))
推荐阅读
- postgresql - PostgreSQL 没有使用直接索引
- symfony - 如何使用 Symfony\Component\Routing\Annotation\Route?
- vb.net - 使用预测试对应用程序进行编码时遇到问题...在 Visual Basic 中循环显示数字列表
- tags - 警告:非法字符串偏移 'tag'
- node.js - 使用Angular 7的Node PUT请求正文为空
- redux - 如何正确测试使用模拟商店调用另一个 thunk 的 thunk
- json - json解析后如何列出未知字段
- node.js - 跨平台配置文件中定义的路径的最佳实践
- swift - 调试信息超出 iPhone X 框架的范围
- python - 在 grpc python 中处理异步流请求