【发布时间】:2020-02-03 11:36:55
【问题描述】:
如何从 Flutter Firestore 中读取子集合。我正在使用 cloud_firestore。我已成功将数据添加到 Firestore,但无法检索它(尝试过但失败了)。
FirebaseUser user=await _firebaseAuth.currentUser();
uid=user.uid;
return user.uid;
}
Future addSpend(Spend spend) {
String date=DateFormat('MM-yyyy').format(spend.date);
print("BLB DB $date");
return Firestore.instance.runTransaction((Transaction transactionHandler) {
return Firestore.instance
.collection("Spends")
.document(uid).collection(date).document()
.setData(spend.toJson());
});
}
我试图将所有子集合读入对象列表。但是失败了。
// QuerySnapshot querySnapshot = await Firestore.instance.collection("Spends").document(Repository.uid).collection("10-2019").getDocuments();
// print("BLB ${querySnapshot.documentChanges}");
// var list = querySnapshot.documents;
// return list;
// List<DocumentSnapshot> templist;
// List<Map<dynamic, dynamic>> list = new List();
// var path=Firestore.instance.collection("Spends").document(uid).collection("10-2019");
//
// var collectionSnapshot=await path.getDocuments();
// print("BLB collection ${collectionSnapshot}");
// templist = collectionSnapshot.documents;
// print("BLB templist ${templist.length}");
// list = templist.map((DocumentSnapshot docSnapshot){
// return docSnapshot.data;
// }).toList();
//
// return list;
var doc = await Firestore.instance.collection('Spends').reference();
doc.getDocuments().then((res){
print("BLB DB ${res.documentChanges.length}");
}).catchError((e){
print("BLB DB error $e");
});
// doc.then((document){
// print("BLB DB ${document.exists}");
// }).catchError((e){
// print("BLB DB error $e");
// });
}
模型类
Spend();
String id,amount; String title; String category; DateTime date; String description;
Spend.fromSnapshot(DocumentSnapshot snapshot)
: id = snapshot.documentID,
amount = snapshot['amount'],
category = snapshot['category'],
date = snapshot['date'],
title = snapshot['title'],
description = snapshot['description'];
toJson(){
return {
"amount":amount,
"category":category,
"date":date,
"description":description,
"title":title
};
}
}
有人可以通过一些光线来了解我吗?我想将该子集合读入列表。我正在使用 cloud_firestore:^0.12.9+4。
【问题讨论】:
-
你有错误吗?
Firestore.instance.collection(...).document(uid).collection(...)此代码应该可以工作。在我的颤振应用程序中,我也在使用 firebase,并且我成功地使用该代码QuerySnapshot querySnapshot = await Firestore.instance.collection('user_groups').document(user.userId).collection('groups').getDocuments()带来了数据 -
没有错误``` print(querySnapshot.documents.length);```但是输出0。
标签: list flutter collections google-cloud-firestore