【问题标题】:How to get a DocumentSnapshot to query by document ID如何获取 DocumentSnapshot 以按文档 ID 查询
【发布时间】:2020-04-24 09:21:42
【问题描述】:

我必须通过刚刚创建的 documentID 查询集合以查找文档,但我需要获取并传递 DocumentSnapshot 而不是 DocumentReference。我怎么才能得到它? 我有这个

   if (_pfKey.currentState.validate()) {
      _pfKey.currentState.save();
      String ragsoc = _cognome + " " + _nome;
     //   DocumentReference ds =Firestore.instance.collection('pazienti').document(this.record);

      for (i = 0; i < ragsoc.length; i++) {
        indici.add(ragsoc.substring(0, i + 1).toLowerCase());
      }
      Map<String, dynamic> miorecord = {
        "nome": _nome,
        "cognome": _cognome,
        "datanascita": _datanascita,
        "luogonascita": _luogonascita,
        "codicefiscale": _codfisc,
        "cap": _cap,
        "provincia": _provincia,
        "indirizzo": _indirizzo,
        "citta": _citta,
        "ciclo": _ciclo,
        "figli": _figli,
        "patologiemam": _patomamm,
        "tormonale": _tormonale,
        "searchIndex": indici
      };


     DocumentReference ds =  await Firestore.instance.collection('pazienti').add(miorecord).catchError((e) {
        print(e);
      }); 

       DocumentSnapshot documentSnapshot = ds.firestore.collection('pazienti').document(ds.documentID);

谢谢你:/

【问题讨论】:

    标签: flutter google-cloud-firestore


    【解决方案1】:

    尝试使用您自己的 customId 保存数据 并使用该 ID 进行检索。

    试试这个:-

    Firestore _fireStore = Firestore.instance;
    
    
    await _fireStore
          .collection('fireStoreCollectionName')
          .document('customId')
          .setData(miorecord);
    
    DocumentSnapshot documentSnapshot = await _fireStore
          .collection('fireStoreCollectionName')
          .document('customId')
          .get();
    
    Map mapName = documentSnapshot.data;
    

    【讨论】:

    • 谢谢。我只是错过了 await 和 .get();
    猜你喜欢
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    • 2020-09-09
    • 2021-09-09
    • 1970-01-01
    • 1970-01-01
    • 2018-07-06
    • 2019-12-28
    相关资源
    最近更新 更多