How to display a list of data from a map ? - flutter firestore

Viewed 610

I am building an event management app. When the user buys a ticket, the ticket details are being saved in the DB like this. Users -> (Current User ID) -> (map)bookedEvents -> eventId -> [event details]

enter image description here

I managed to display data of one specific event when I give one of the Event IDs as a static String like this.

StreamBuilder(
          stream: FirebaseFirestore.instance
              .collection('users')
              .doc(_uid)
              .snapshots(),
          builder: (context, snapshot) {
            if (!snapshot.hasData) {
              return Text("Loading");
            }
            var userDocument = snapshot.data;
            return Text(
              userDocument["bookedEvents"]["KFXvCj63y7GTjQcMKfVy"]
                  ["eventName"],
              style: TextStyle(fontWeight: FontWeight.bold, fontSize: 20),
            );
          }),

But I want to display all the Event Names in a list. Could I know how? Thank you so much.

1 Answers

You should try something like that:

var userDocument = snapshot.data;

final List<Widget> children = [];

userDocument['bookedEvents'].forEach((key, value) { 
children.add(Text(value['eventName'])); });   
 
return Column(children: children); // or Row(children: children); or ListView(children: children);
Related