Home > Mobile >  Accessing data from a sub collection in Firebase
Accessing data from a sub collection in Firebase

Time:01-13

I have a collection named 'room', inside which I've a document named 'users', inside the doc I've another collection named 'users' inside which I've filed named 'userName'. I want to access the 'userName'

Image 1, Image 2, Image 3

Heres what I've tried

       StreamBuilder(
                    stream:  FirebaseFirestore.instance.collection(widget.roomId).doc("users").collection("users").snapshots(),
                    builder: (context, newSnapshot){
                      final documents = newSnapshot.data?.docs;
                      if (newSnapshot.connectionState ==  ConnectionState.waiting){
                        return Text("WAITING"); 
                      }
                     return ListView.builder(
                          itemCount: documents?.length,
                          itemBuilder: (context, index) => Container(
                            height: 25,
                            child: Text(documents![index]["usersName"] ?? "nothing found"),
                          ));
                    },
                  ),

CodePudding user response:

Seems like you have misspelled the userName key in your text widget

child: Text(documents![index['usersName']??'Nothing found');

should be

child: Text(documents![index]['userName']??'Nothing found');
  • Related