Joh*_*ohn 3 dart firebase flutter google-cloud-firestore
我有一个颤振聊天,当用户到达列表末尾时显示最新消息。
这是通过在 firestore 上设置一个侦听器来完成的,限制为 10 条消息(限制(10)),当我到达消息列表的末尾时,我将流的限制增加 10。
// LISTENER QUERY MESSAGES
limitdocuments = 10;
StreamBuilder(
stream: Firestore.instance
.collection('groups')
.document(groupId)
.collection("messages")
.orderBy('sentTime', descending: true)
.limit(limitdocuments)
.snapshots(),
builder: (context, snapshot) {
if (!snapshot.hasData) {
return Center(
child: CircularProgressIndicator(valueColor: AlwaysStoppedAnimation<Color>(themeColor)));
} else {
return ListView.builder(
itemBuilder: (context, index) {
buildItem(index, snapshot.data.documents[index],true)
},
itemCount: snapshot.data.documents.length,
reverse: true,
controller: listScrollController,
);
}
},
),
Run Code Online (Sandbox Code Playgroud)
// END OF THE LIST, INCREASE THE LIMIT BY 10
if(listScrollController.position.atEdge){
if(listScrollController.position.pixels == 0){
setState(() {
limitdocuments= limitdocuments+ 10;
});
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题是,firestore 是从数据库中重新下载所有消息(包括旧消息)还是从缓存中获取旧消息?
谢谢
首先
initState()以便您确定它只是在小部件实例化时被调用。否则,它将始终尝试创建新的流集。接下来是测试
尝试手动打印日志以查看您期望的行为是否正确。
for (int i = 0; i < snapshot.data.documents.length; i++) {
DocumentSnapshot doc = snapshot.data.documents.elementAt(i);
// Check manually if the data you're referring to is coming from the cache.
print(doc.metadata.isFromCache ? "Cached" : "Not Cached");
}
Run Code Online (Sandbox Code Playgroud)
最后,是的——它应该被缓存。
从 Firebase文档:
Cloud Firestore 支持离线数据持久化。此功能会缓存您的应用正在使用的 Cloud Firestore 数据的副本,以便您的应用可以在设备离线时访问这些数据。您可以写入、读取、侦听和查询缓存数据。当设备重新上线时,Cloud Firestore 会将您的应用所做的任何本地更改同步到 Cloud Firestore 后端。
据我所知,Flutter Firestore 或实时数据库插件应该遵循与原生和 Web 对应 SDK 相同的架构和行为。
| 归档时间: |
|
| 查看次数: |
2391 次 |
| 最近记录: |