Flutter 扑动燃烧室-方法';文件';isn';t为类型';溪流';
我试图迭代文档中的集合(使用for循环),并从每个集合文档中获取和捕获数据。当我第一次尝试这样做时,我使用了一个流来侦听数据,但是由于异步函数的缘故,该函数总是返回一个空数组 我的代码Flutter 扑动燃烧室-方法';文件';isn';t为类型';溪流';,flutter,asynchronous,google-cloud-firestore,Flutter,Asynchronous,Google Cloud Firestore,我试图迭代文档中的集合(使用for循环),并从每个集合文档中获取和捕获数据。当我第一次尝试这样做时,我使用了一个流来侦听数据,但是由于异步函数的缘故,该函数总是返回一个空数组 我的代码 for (final day in daysRunOn) { databaseReference .collection('users') .document(user.uid) .collection(day) .snapshots() .docu
for (final day in daysRunOn) {
databaseReference
.collection('users')
.document(user.uid)
.collection(day)
.snapshots()
.documents((snapshot) {
});
}
建议我不应该使用流,所以我删除了它。我现在正在尝试运行此程序,但在.documents部分不断出现错误,该错误表示“没有为类型“Stream”定义方法“documents”。”但我不理解这仍然是一个流的方式或原因
堆栈溢出的建议方法
double queryValues() async {
total = 0.0;
docs = await Firestore.instance
.collection('myCollection')
.snapshots()
.documents((snapshot);
docs.forEach((doc) => this.total += doc.data['amount']));
debugPrint(this.total.toString());
return total;
}
任何帮助都将不胜感激 试试这段代码
List<String> daysRunOn = ['day1','day2','...'];
int x = 0;
double total = 0.0;
Future<double> getTotal() async {
if(x == 0) total = 0.0;
var querySnapshot = await
Firestore.instance.collection('users/${user.uid}/${daysRunOn[x]}').getDocuments();
querySnapshot.documents.forEach((doc) => total += doc.data['amount']);
if(x == daysRunOn.length-1){
// the loop has finished
x = 0;
debugPrint(total.toString());
return total;
} else {
x++;
return getTotal();
}
}
List daysRunOn=['day1'、'day2'、'…];
int x=0;
双倍合计=0.0;
Future getTotal()异步{
如果(x==0)总计=0.0;
var querySnapshot=wait
Firestore.instance.collection('users/${user.uid}/${daysRunOn[x]}').getDocuments();
querySnapshot.documents.forEach((doc)=>total+=doc.data['amount']);
如果(x==daysRunOn.length-1){
//循环结束了
x=0;
debugPrint(total.toString());
返回总数;
}否则{
x++;
返回getTotal();
}
}
尝试以下方法:
getDocs() async {
total = 0.0;
snapshot = await Firestore.instance
.collection('myCollection')
.snapshots();
docs = snapshot.data.documents;
docs.forEach((doc) => this.total += doc.data['amount']));
debugPrint(this.total.toString());
return total;
}