简体   繁体   中英

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. 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.

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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