首页 > 解决方案 > 颤振:获取云火库中项目的长度

问题描述

我对flutter和firestore完全陌生。所以请帮忙。我想从userCarts中获取Firestore中拥有多少项目。在当前的uid中,userCart中有3个项目。所以我想得到结果为3防火结构

QuerySnapshot s= FirebaseFirestore.instance.collection('users').doc(user.uid).get();
int docs=s.docs.length;

标签: firebasefluttergoogle-cloud-firestore

解决方案


首先,当你打电话时, QuerySnapshot s= FirebaseFirestore.instance.collection('users').doc(user.uid).get(); 你没有得到 QuerySnapshot,你得到的是 DocumentSnapshot,你应该这样做

DocumentSnapshot doc= await FirebaseFirestore.instance.collection('users').doc(user.uid).get();

List<dynamic> userCart = doc.data()['userCart'];//here you already got the list

int length = userCart.length;

注意 - 我使用过await,所以这应该在异步函数中。


推荐阅读