从 Firestore Flutter 中读取子集合

BLB*_*BLB 2 collections list flutter google-cloud-firestore

如何从 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());
    });
  }
Run Code Online (Sandbox Code Playgroud)

我试图将所有子集合读入对象列表。但是失败了。

//    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");
//   });
  }
Run Code Online (Sandbox Code Playgroud)

模型类

  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

    };
  }
}
Run Code Online (Sandbox Code Playgroud)

Firebase 数据库 Firebase 数据库连续...

有人可以通过一些光照我。我想将该子集合读入列表。我正在使用 cloud_firestore:^0.12.9+4。

小智 5

你只想

Firestore.collection("collection/documentid/subcollectionname').getdocuments
Run Code Online (Sandbox Code Playgroud)


tzo*_*urn 2

这种行为的原因有很多。一种可能是您查询不存在的文档

\n\n

还要考虑用于列出文档子集合的 Firestore 文档

\n\n

考虑到这一点,尝试使用此代码片段可能会解决您的问题:

\n\n
var spendsRef = await Firestore.instance.document(\'Spends/YOUR_DOCUMENT_ID\');\nspendsRed.getCollections() => then(collections => {\n    collections.forEach(collection => {\n    console.log(\'Found subcollection with id:\', collection_id);\n });\n});\n
Run Code Online (Sandbox Code Playgroud)\n\n

让我知道这是否有帮助。

\n