【问题标题】:Flutter No Method `get` in DocumentSnapshot在 DocumentSnapshot 中颤振没有方法`get`
【发布时间】:2020-04-13 11:40:27
【问题描述】:

尽管有 here 的文档,Flutter 还是给了我错误:

The method 'get' isn't defined for the class 'DocumentSnapshot'.

这是我的代码:

  Future<Object> getFromDocumentSnapshot(String email, String field) async {
    Firestore.instance
        .collection('users')
        .document(email)
        .get()
        .then((DocumentSnapshot ds) {
      return ds.get(field);
    });
  }

不确定这个。有什么明显的问题吗?谢谢!

【问题讨论】:

    标签: mysql firebase flutter dart google-cloud-firestore


    【解决方案1】:

    您引用的链接是针对 javascript 而不是使用 dart 编程语言的颤振。如果您想在 Flutter 中获取文档,则必须执行以下操作:

    Firestore.instance
            .collection('talks')
            .document('document-name')
            .get()
            .then((DocumentSnapshot ds) {
          // use ds as a snapshot
        });
    

    你需要使用插件cloud_firestore:

    https://pub.dev/packages/cloud_firestore

    如您所见,DocumentSnapshot 不包含方法get()

    https://github.com/FirebaseExtended/flutterfire/blob/master/packages/cloud_firestore/cloud_firestore/lib/src/document_snapshot.dart

    【讨论】:

      【解决方案2】:

      要访问DocumentSnapshot object 中的数据,请使用其data 属性。例如:

      Firestore.instance
          .collection('users')
          .document(email)
          .get()
          .then((DocumentSnapshot ds) {
        return ds.data[field];
      });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-01-17
        • 2019-02-26
        • 2021-08-06
        • 2021-11-20
        • 2021-07-10
        • 1970-01-01
        • 2020-09-03
        相关资源
        最近更新 更多