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

时间:2021-07-23 21:04:20

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

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

2 个答案:

答案 0 :(得分:2)

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

allow

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

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

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

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

答案 1 :(得分:0)

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