【问题标题】:Read subcollection from Firestore flutter从 Firestore Flutter 中读取子集合
【发布时间】: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


【解决方案1】:

你只是想

Firestore.collection("collection/documentid/subcollectionname').getdocuments

【讨论】:

    【解决方案2】:

    这种行为有很多原因。一个可以是你query for documents that don’t exist。

    还要考虑Firestore documentation for Listing subcollections of a document。

    考虑到这一点,也许尝试使用这个 sn-p 将解决您的问题:

    var spendsRef = await Firestore.instance.document('Spends/YOUR_DOCUMENT_ID');
    spendsRed.getCollections() => then(collections => {
        collections.forEach(collection => {
        console.log('Found subcollection with id:', collection_id);
     });
    });
    

    如果这有帮助,请告诉我。

    【讨论】:

    • 我已添加数据库屏幕截图。你能检查一下吗。您的代码给出错误(无法编译)。
    • 在您的第一个屏幕截图中,“此文档不存在并且不会出现在查询或快照中”的消息证实了我的第一个假设。您正在尝试查询不存在的文档。请查看Non-existent ancestor documents
    • 谢谢兄弟。在 Firestore 中添加子集合的任何想法?
    • Here 是官方文档。 This 也可能有帮助。让我知道这是否适合您。
    • 由于我的回答是正确的,我建议您批准它,以便让其他用户看到。
    猜你喜欢
    • 2021-06-30
    • 2021-08-02
    • 1970-01-01
    • 2020-06-08
    • 2021-06-28
    • 1970-01-01
    • 2019-04-30
    • 2022-11-01
    • 2020-09-05
    相关资源
    最近更新 更多