BLB
BLB

Reputation: 893

Read subcollection from Firestore flutter

How to read subcollection from flutter firestore. I am using cloud_firestore. I am successfully adding data into firestore but couldn't retrieve it(tried and failed).

    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());
    });
  }

I tried to read all that subcollections into list of objects. But failed.

//    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");
//   });
  }

Model class

  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

    };
  }
}

Firebase database Firebase DB conti..

Can someone through some light on me. I want to read that subcollection into list. I am using cloud_firestore: ^0.12.9+4.

Upvotes: 5

Views: 9482

Answers (2)

user14089096
user14089096

Reputation:

You just want to

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

Upvotes: 6

tzovourn
tzovourn

Reputation: 1321

There are a lot of reasons for this behaviour. One can be that you query for documents that don’t exist.

Also take into consideration Firestore documentation for Listing subcollections of a document.

Taking this into account maybe trying to use this snippet will resolve your issue:

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);
 });
});

Let me know if this was helpful.

Upvotes: 2

Related Questions