【问题标题】:How to get data from firestore document fields in flutter?如何从flutter中的firestore文档字段中获取数据?
【发布时间】:2019-11-27 08:28:54
【问题描述】:

我正在尝试在我的 Flutter 应用中测试 Firestore,但我无法真正按预期加载数据。

这就是我获取数据的方式:

 Firestore.instance
        .collection('Test')
        .document('test')
        .get()
        .then((DocumentSnapshot ds) {
      // use ds as a snapshot

      print('Values from db: $ds');
    });

打印语句输出:flutter: Values from db: 'DocumentSnapshot' 的实例。

我也尝试了以下方法,但没有成功:

Firestore.instance.collection('Test').document('test').get().then((data) {
  // use ds as a snapshot

  if (data.documentID.length > 0) {
    setState(() {
      stackOver = data.documentID;
      print(stackOver);
    });
  }
});

这里的输出只是documentID-name..

那么,我如何从 firestore 获取字段值?

最好的问候!

【问题讨论】:

  • 尝试使用 ds["fieldname"] 或 ds.fieldname
  • 你有哪些字段?意思是你有什么型号,你能告诉我吗?
  • @MuhammadNoman Test(Collection) -> test(document) -> stackTest: value(field-ref and value)
  • @ParthPatel 谢谢,它适用于你的方法。 :)
  • @RusbenWladiskoz 这是我的荣幸。你可以标记和支持我的评论:)

标签: firebase flutter dart google-cloud-firestore


【解决方案1】:

空安全码:

  • 一次性读取数据:

    var collection = FirebaseFirestore.instance.collection('Test');
    var docSnapshot = await collection.doc('test').get();
    if (docSnapshot.exists) {
      Map<String, dynamic>? data = docSnapshot.data();
    
      // You can then retrieve the value from the Map like this:
      var value = data?['your_field'];
    }
    
  • 持续监听数据:

    var collection = FirebaseFirestore.instance.collection('Test');
    collection.doc('test').snapshots().listen((docSnapshot) {
      Map<String, dynamic>? data = docSnapshot.data();
      if (docSnapshot.exists) {
        Map<String, dynamic>? data = docSnapshot.data();
    
        // You can then retrieve the value from the Map like this:
        var value = data?['your_field'];
      }
    });
    

【讨论】:

    【解决方案2】:

    我遇到了类似的问题并以这种方式解决了它:

    Stream<DocumentSnapshot> provideDocumentFieldStream() {
        return Firestore.instance
            .collection('collection')
            .document('document')
            .snapshots();
    }
    

    之后,我使用了一个 StreamBuilder 来使用上面创建的方法:

    StreamBuilder<DocumentSnapshot>(
        stream: provideDocumentFieldStream(),
        builder: (BuildContext context, AsyncSnapshot<DocumentSnapshot> snapshot) {
            if (snapshot.hasData) {
                //snapshot -> AsyncSnapshot of DocumentSnapshot
                //snapshot.data -> DocumentSnapshot
                //snapshot.data.data -> Map of fields that you need :)
    
                Map<String, dynamic> documentFields = snapshot.data.data;
                //TODO Okay, now you can use documentFields (json) as needed
    
                return Text(documentFields['field_that_i_need']);
            }
        }
    )
    

    【讨论】:

      【解决方案3】:

      试试这个:

      final String _collection = 'collectionName';
      final Firestore _fireStore = Firestore.instance;
      
      getData() async {
        return await _fireStore.collection(_collection).getDocuments();
      }
      
      getData().then((val){
          if(val.documents.length > 0){
              print(val.documents[0].data["field"]);
          }
          else{
              print("Not Found");
          }
      });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-12-05
        • 2020-09-05
        • 2021-11-05
        • 2021-10-21
        • 2022-12-13
        • 2020-09-06
        • 2021-05-27
        相关资源
        最近更新 更多