【问题标题】:Flutter FireStore, Get document by Document keyFlutter FireStore,通过文档键获取文档
【发布时间】:2018-08-21 22:18:05
【问题描述】:

通过 .documents[x] 获取文档,其中 x 是从零开始的文档编号,在此示例中有效。如何通过键值获取文档? 例如,第 3 个文档的键为“qwwe”

我怎样才能得到这份文件?
Snapshot.data.document["qwwe"]?

这是与代码问题代码一起工作的代码 // out.

//=======   this works
//      body: new ListOfNACS01(),

        body: new StreamBuilder(
//          stream: Firestore.instance.collection('COMPLEX_NONACS').document('A01B01C01D02').snapshots(),
          stream: Firestore.instance.collection('COMPLEX_NONACS').snapshots(),
          builder: (context, snapshot){
            if (!snapshot.hasData) return const Text("---  loading ---");
//            return new ListView.builder(
//              itemCount: snapshot.data.document.length,
//            );

              return Column(
                children: <Widget>[
                  Text(snapshot.data.documents[0].documentID),
                  Text(snapshot.data.documents[0]["DE02PRIMARY"]["INDICATION"]),


                  //--------  this does not work  ------------
                  //  how to display a document by DocumentID?
// --  The below line gives error of... 
// error: Too many positional arguments: 1 expected, but 2 found. 
// (extra_positional_arguments_could_be_named at [scaiqit55] 
// lib/main.dart:48).

                  Text(snapshot.data.document['A01B01C01D01']),
                  Text(snapshot.data.documents[0]["DE02PRIMARY"]["INDICATION"]),


                ],


              );
          },
        ),
    );
  }
}

【问题讨论】:

标签: flutter google-cloud-firestore


【解决方案1】:

如果您指的是使用 ID 获取文档快照,以下是使用版本 ^0.14.3 获取 Cloud Firestore 文档快照的示例。我还添加了一个示例,说明如何从文档快照中获取键值(如果这是您要查找的内容)。您可以访问FlutterFire cloud_firestore guide了解更多详情。

// Collection reference
FirebaseFirestore.instance.collection('fruits')
  // Document snapshot fetched using ID
  .doc('AQbq33GJ9GE09OaChOPq')
  // Fetches the value inside the Document snapshot using a key
  // value contains a HashMap from the Document snapshot
  .get().then((value) => debugPrint('Fruit name: ${value.data()['name']}'));

这是从 Firestore 获取的数据

debugPrint() 的预期输出是 'Fruit name: Cherry'

【讨论】:

    猜你喜欢
    • 2021-07-20
    • 2021-01-28
    • 2021-06-17
    • 2021-11-19
    • 2019-05-05
    • 2020-12-05
    • 2021-09-23
    • 2019-09-17
    • 2020-02-24
    相关资源
    最近更新 更多