【问题标题】:how can i get single firestore document dynamically in flutter如何在颤动中动态获取单个 Firestore 文档
【发布时间】:2021-09-30 22:13:09
【问题描述】:

我在 listveiw.builder 小部件中显示了一个字符串列表,当我按下一个单词时,我想从 firebase 获取文档信息

var words = [red,blue,green,yellow] ;

我想根据列表的值获取文档

显示在:

 ListView.builder(
                    itemCount: words.length,
                    itemBuilder: (context, index) {
                      return
                          Column(
                            children: [
                              Row(
                                children: [
                                  Expanded(
                                    flex: 3,
                                    child: InkWell(
                                      onTap: () {},
                                      child: Text('${words[index]}'),
                                    ),
                                  ),
                                  Expanded(
                                    flex: 1,
                                    child: InkWell(
                                      onTap: () {
                                       // press this and get the data of {words[index]} document
                                      },
                                      child: Text(
                                        'press to get color info',
                                      ),
                                    ),
                                  ),
                                ],
                              ),
                              Text( show the data here after fetching it from firestore)
                            ],
                          );
                    },
                  )

【问题讨论】:

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


    【解决方案1】:

    您可以使用这样的文档 ID 读取单个文档

    class GetUserName extends StatelessWidget {
        final String documentId;
    
        GetUserName(this.documentId);
    
    @override
    Widget build(BuildContext context) {
        CollectionReference users  =FirebaseFirestore.instance.collection('users');
    
    return FutureBuilder<DocumentSnapshot>(
      future: users.doc(documentId).get(),
      builder:
          (BuildContext context, AsyncSnapshot<DocumentSnapshot> snapshot) {
    
        if (snapshot.hasError) {
          return Text("Something went wrong");
        }
    
        if (snapshot.hasData && !snapshot.data!.exists) {
          return Text("Document does not exist");
        }
    
        if (snapshot.connectionState == ConnectionState.done) {
          Map<String, dynamic> data = snapshot.data!.data() as Map<String, dynamic>;
          return Text("Full Name: ${data['full_name']} ${data['last_name']}");
        }
    
        return Text("loading");
      },
    );
    

    } }

    欲了解更多信息,请访问https://firebase.flutter.dev/docs/firestore/usage

    【讨论】:

      【解决方案2】:

      假设您有一个 Firestore,其中包含名为“words”的集合

      CollectionReference wordsFireStoreRef = FirebaseFirestore.instance.collection('words');
      

      现在您必须访问包含您要查找的单词的集合中的文档。

      ListView.builder(
                          itemCount: words.length,
                          itemBuilder: (context, index) {
                            return
                                Column(
                                  children: [
                                    Row(
                                      children: [
                                        Expanded(
                                          flex: 3,
                                          child: InkWell(
                                            onTap: () {},
                                            child: Text('${words[index]}'),
                                          ),
                                        ),
                                        Expanded(
                                          flex: 1,
                                          child: InkWell(
                                            onTap: () {
                                             // press this and get the data of {words[index]} document
                                            // LOOK HERE. This line will return  a type called Future<DocumentSnapshot>
                                             wordsFireStoreRef.doc(words[index]).get();
                                            },
                                            child: Text(
                                              'press to get color info',
                                            ),
                                          ),
                                        ),
                                      ],
                                    ),
                                    Text( show the data here after fetching it from firestore)
                                  ],
                                );
                          },
                        )
      

      剩下的交给你。您可以使用 FutureBuilder 构建一个小部件,也可以这样做

      wordsFireStoreRef.doc(words[index]).get().then((value){
          //Value is the data that was returned
          print(value.data()['word']);
      });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-01-26
        • 2021-05-13
        • 2021-08-06
        • 1970-01-01
        • 2020-11-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多