【发布时间】:2020-02-15 16:51:41
【问题描述】:
我有一个异步方法来返回带有条件的firebase查询的文档长度,并且已经在方法中使用await实现。代码如下。
/////////////////////
getUreadMsgCount(groupChatId, id).then((result) {
unReadCount = result;
print("COUNT WITHIN getUreadMsgCount Method : $unReadCount");
});
print("UNREAD OUTSIDE METHOD : $unReadCount");
////////////////////////////////
Future getUreadMsgCount(String groupId, String idFrom) async {
var respectsQuery = Firestore.instance
.collection('messages')
.document(groupId).collection(groupId)
.where('idFrom', isEqualTo: idFrom).where('isSeen', isEqualTo: 0);
var querySnapshot = await respectsQuery.getDocuments();
int totalUnread = querySnapshot.documents.length;
return totalUnread;
}
////////////OUT PUT/////////////////////
I/flutter (28972): UNREAD OUTSIDE METHOD : 0
I/flutter (28972): UNREAD OUTSIDE METHOD : 0
I/flutter (28972): UNREAD OUTSIDE METHOD : 0
I/flutter (28972): COUNT WITHIN getUreadMsgCount Method : 2
I/flutter (28972): COUNT WITHIN getUreadMsgCount Method : 0
I/flutter (28972): COUNT WITHIN getUreadMsgCount Method : 0
如果你注意到,它总是首先执行 OUTSIDE METHOD 行,因此总是得到值为 0
我需要先从初始化的 getUreadMsgCount 方法中获取值,然后继续下一行。有什么帮助吗?
【问题讨论】:
标签: firebase flutter dart google-cloud-firestore