Solved: Flutter How to get all docs and collections

Question

Asked by Tolga Y on December 15, 2021 (source).

I need to get my data as in the path I am showing.

shipment collection > collection id (also this is user id) > myYuks collection > doc's id > doc's field

enter image description here

enter image description here

I have a YukModel and I have a Database Service.

My YukModel codes;
class YukModel {
  String? yukID;
  String? yukBaslik;
  String? icerik;
  int? agirlik;
  Timestamp? createTime;
  String? aracTipi;
  bool? onayDurumu;

  YukModel(
      {this.yukID,
      this.yukBaslik,
      this.icerik,
      this.agirlik,
      this.createTime,
      this.aracTipi,
      this.onayDurumu});

  YukModel.fromDocumentSnapshot(DocumentSnapshot documentSnapshot) {
    yukID = documentSnapshot.id;
    yukBaslik = documentSnapshot.get('yukBaslik');
    icerik = documentSnapshot.get('icerik');
    agirlik = documentSnapshot.get('agirlik');
    createTime = documentSnapshot.get('createTime');
    aracTipi = documentSnapshot.get('aracTipi');
    onayDurumu = documentSnapshot.get('onayDurumu');
  }
}

A fragment of my Database Code;

class DataBaseFB {
  final FirebaseFirestore _firebaseFirestore = FirebaseFirestore.instance;
  


getYukListFromDB() {
    _firebaseFirestore.collection("shipment").get().then((querySnapshot) async {
      for (var resultA in querySnapshot.docs) {
        await _firebaseFirestore
            .collection("shipment")
            .doc(resultA.id)
            .collection("myYuks")
            .get()
            .then((querySnapshot) async {
          for (var resultB in querySnapshot.docs) {
            await _firebaseFirestore
                .collection("shipment")
                .doc(resultA.id)
                .collection("myYuks")
                .doc(resultB.id)
                .get()
                .then((value) {
              YukModel yukModel = YukModel.fromDocumentSnapshot(value);
              debugPrint("-----------------------YUKLER---------------\n"
                  "yukBaslik: ${yukModel.yukID}\n"
                  "iccerik: ${yukModel.icerik}");
            });
          }
        });
      }
    });
  }

}

And I tried to acces getYukListFromDB with a button in HomePage. Then I got this error;

Exception has occurred. StateError (Bad state: field does not exist within the DocumentSnapshotPlatform)

What should I do ?

Answer

Question answered by Tolga Y (source).

I solved problem myself. Here is code from Database file;

getYukListFromDB() async {
    _firebaseFirestore
        .collection('shipment')
        .get()
        .then((QuerySnapshot querySnapshot) {
      for (var docA in querySnapshot.docs) {
        debugPrint("shipment altındaki docs idsi = " + docA.id);
        _firebaseFirestore
            .collection('shipment')
            .doc(docA.id)
            .collection('myYuks')
            .get()
            .then((QuerySnapshot querySnapshot) {
          for (var docB in querySnapshot.docs) {
            debugPrint("myYuk altındaki docs idsi = " + docB.id);
            _firebaseFirestore
                .collection('shipment')
                .doc(docA.id)
                .collection('myYuks')
                .doc(docB.id)
                .get()
                .then((DocumentSnapshot documentSnapshot) {
              Map<String, dynamic> mapData =
                  documentSnapshot.data()! as Map<String, dynamic>;
              if (documentSnapshot.exists) {
                debugPrint("icerik =  ${mapData['icerik']}");
                debugPrint('doc var');
              } else {
                debugPrint('doc yok');
              }
            });
          }
        });
      }
    });
FIREBASE FLUTTER GOOGLE-CLOUD-FIRESTORE
SHARE: