【问题标题】:Flutter & Firebase : How do I get a specific field from a specific document into a stream?Flutter & Firebase:如何从特定文档中获取特定字段到流中?
【发布时间】:2021-02-01 21:54:20
【问题描述】:

我一直在尝试将特定文档中的特定字段放入流中。但我总是为空。我该如何解决这个问题?

功能

Stream<List<String>> getError() {
    DocumentReference errorReference =
        appSettingCollection.document('report_error');

    final Stream<DocumentSnapshot> snapshots = errorReference.snapshots();

    return snapshots.map((doc) {
      //print(doc.data['types']);
      return doc.data['types'];
    });
  }

主要

return StreamBuilder<List<String>>(
        stream: User_DatabaseService().getError(),
        builder: (context, snapshot) {
          
          final List<String> errorTypes = snapshot.data;
          print('In Error Report : $errorTypes'); // I got null??? But why?
///--------

}}

【问题讨论】:

    标签: firebase flutter stream


    【解决方案1】:

    我认为你采取了错误的流。 尝试纠正 Stream

    StreamBuilder<DocumentSnapshot>(
          stream: Firestore.instance
              .collection('CollectionName')
              .document("DocumentID")
              .get(),
    

    并在下面使用它来访问特定字段:

    document["FieldName"]
    

    要访问单个字段,您需要访问整个文档。我建议使用 FutureBuilder 按照下面的链接,您会得到更准确的答案。

    https://firebase.flutter.dev/docs/firestore/usage/

    【讨论】:

    • cloud_firestore 在 0.14.0 上有重大更改,弃用了许多方法,但想法应该相似
    猜你喜欢
    • 2020-09-20
    • 2021-07-04
    • 2020-11-05
    • 2021-09-15
    • 2021-07-20
    • 2018-09-04
    • 2021-07-22
    • 1970-01-01
    • 2022-12-20
    相关资源
    最近更新 更多