简体   繁体   English

从 flutter 监听 firestore 数据库中的两个 collections

[英]Listen to two collections in firestore database from flutter

I have been trying to listen to two different collection documents.我一直在尝试聆听两种不同的收藏文件。 One is shown below.一个如下所示。 It basically writes the real-time data to the screen.它基本上是将实时数据写入屏幕。 I want to listen to the other collection as well but execute a particular function instead.我也想听其他集合,但改为执行特定的 function。 How should I proceed, please?请问我该怎么办? Truly appreciate any help!!非常感谢任何帮助!

Expanded(
                      child: StreamBuilder(
                        stream:
                        FirebaseFirestore.instance.collection('location').snapshots(),
                        builder: (context, AsyncSnapshot<QuerySnapshot> snapshot) {
                          if (!snapshot.hasData) {
                            return Center(child: CircularProgressIndicator());
                          }
                          return ListView.builder(
                              itemCount: snapshot.data?.docs.length,
                              itemBuilder: (context, index) {
                                return ListTile(
                                  title:
                                  Text(snapshot.data!.docs[index]['name'].toString()),
                                  subtitle: Row(
                                    children: [
                                      Text(snapshot.data!.docs[index]['latitude']
                                          .toString()),
                                      SizedBox(
                                        width: 20,
                                      ),
                                      Text(snapshot.data!.docs[index]['longitude']
                                          .toString()),
                                    ],
                                  ),
                                  trailing: IconButton(
                                    icon: Icon(Icons.directions),
                                    onPressed: () {
                                      Navigator.of(context).push(MaterialPageRoute(
                                          builder: (context) =>
                                              MyMap(snapshot.data!.docs[index].id)));
                                    },
                                  ),
                                );
                              });
                        },
                      )),

You can listen to other firebase collections and call your functions like this.您可以收听其他 firebase collections 并像这样调用您的函数。

FirebaseFirestore.instance
    .collection('location')
    .snapshots()
    .listen((QuerySnapshot querySnapshot) {

  final  firestoreList = querySnapshot.docs;
  print(firestoreList.first.data());

}).onError((e) => print(e));

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

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