【发布时间】:2021-03-10 04:55:08
【问题描述】:
我正在尝试计算 firestore 映射内所有数组中的所有元素,以获得数字 3 的结果。
我使用下面的函数来获得计算chatRoomIds字段内所有元素的结果。
int _getUnseenMessagesNumber(List<Map<String, dynamic>> items) {
int counter = 0;
for (final item in items) {
counter += item.values.first.length;
}
return counter;
}
最后我试着用下面的代码来显示计数的结果。
StreamBuilder<DocumentSnapshot>(
stream: FirebaseFirestore.instance
.collection('crews')
.doc(uid)
.snapshots(),
builder: (context, snapshot) {
int number = _getUnseenMessagesNumber(
snapshot.data['chatRoomIds']);
if (snapshot.data['chatRoomIds'] == null ||
number == 0) {
return null;
} else {
return Center(
child: Text(
number.toString(),
);
}
})
当我尝试上面的代码时,我收到一条错误消息type '_internalLinkedHashMap <String, dynamic> ' is not a subtype of type 'List<Map<String, dynamic>> '。
要解决什么问题才能避免此错误并获得我想要的结果?
【问题讨论】:
标签: flutter dictionary google-cloud-firestore count