繁体   English   中英

如何在颤振中动态获取单个 Firestore 文档

[英]how can i get single firestore document dynamically in flutter

我在 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)
                            ],
                          );
                    },
                  )

假设您有一个名为“words”的集合的 Firestore

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']);
});

您可以使用这样的文档 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM