【发布时间】:2021-09-06 17:19:16
【问题描述】:
我知道可以使用 GetOptions 命令(来源:Source.cache)来减少不必要的读取。我的问题是,我怎样才能通过缓存运行我的 Firestore 查询,如果那里什么都没有,即缓存是空的,通过服务器?
我的代码将类似于...
_getLikes() {
User _user = _firebaseAuth.currentUser;
FirebaseFirestore.instance.collection('Users')
.doc(_user.uid)
.collection("Likes")
.where('liked', isEqualTo: true)
.snapshots()
.listen((_querySnapshot) {
//likedList.clear();
if (_querySnapshot.docs.isNotEmpty) {
_querySnapshot.docs.forEach((_likedDocumentSnapshot) async {
await FirebaseFirestore.instance.collection('Users')
.doc(_likedDocumentSnapshot.data()['likedId'])
.get()
.then((_usersDocumentSnapshot) {
if (_usersDocumentSnapshot.exists) {
User _tempUser =
User.fromDocument(_usersDocumentSnapshot);
liked.add(_tempUser);
if (mounted) setState(() {});
}
});
});
}
});
}
User 是我的类,我在其中管理服务器的内容,例如用户的姓名和年龄。
我只需要知道如何使用它
GetOptions(source: Source.cache)
里面
get()
如果缓存为空
GetOptions(source: Source.server)
首先我的函数 getLikes() 应该查看数据是否在缓存中并从那里加载。如果它不在缓存中,则应该从服务器加载数据。
非常感谢您的帮助,在 Google 上找不到任何内容。
【问题讨论】:
标签: flutter caching google-cloud-firestore server cloud