【问题标题】:Flutter How to get all docs and collectionsFlutter 如何获取所有文档和集合
【发布时间】:2022-01-18 13:24:30
【问题描述】:

我需要按照我显示的路径获取我的数据。

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

我有一个 YukModel 和一个数据库服务。

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

我的数据库代码片段;

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

}

我尝试使用主页中的按钮访问 getYukListFromDB。然后我得到了这个错误;

发生了异常。 StateError(错误状态:字段不存在 在 DocumentSnapshotPlatform 中)

我该怎么办?

【问题讨论】:

  • 报错说明你要搜索的字段在数据模型中不存在。当您使用 firebase 时,您无需在 Flutter 应用程序中创建模型类,您可以直接收听实时数据库。并从数据库中获取特定数据
  • 但我更喜欢分离我的所有树。因此,这些字段已经存在于 Firebase 和 YukModel 中。抱歉,您的回答并没有解决我的问题。谢谢。

标签: firebase flutter google-cloud-firestore


【解决方案1】:

我自己解决了问题。这是数据库文件中的代码;

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

【讨论】:

    猜你喜欢
    • 2021-03-30
    • 2019-08-05
    • 2021-07-29
    • 1970-01-01
    • 2021-01-06
    • 2018-04-03
    • 1970-01-01
    相关资源
    最近更新 更多