0

I want to retrieve data from Firestore and show them in my flutter app , but the document has a map called 'users' , my question is how I can access to This map field ?

Firebase

StreamBuilder(
    stream: FirebaseFirestore.instance
        .collection('Channels')
        .where('participants', arrayContains: UserInfoData.userID)
        .snapshots(),
    builder: (context, AsyncSnapshot<QuerySnapshot> streamSnapshot) {
      switch (streamSnapshot.connectionState) {
        case ConnectionState.waiting:
          return Center(
            child: CircularProgressIndicator(),
          );

        default:
          if (streamSnapshot.hasError) {
            print(streamSnapshot.hasError.toString());
          } else {
            final channels = streamSnapshot.data!.docs;
            if (channels.isEmpty) {
              return Text('ff');
            } else
              return ListView.builder(
                itemCount: channels.length,
                itemBuilder: (context, i) => ListTile(
                  leading: CircleAvatar(
                    backgroundImage:
                        NetworkImage(channels[i]['announceImage']),
                  ),
                  title: Text(channels[i]['reciverID']),
                ),
              );
          }
      }
      return Text('ok');
    },
  ),
);
2
  • The same way that you access to another attributes: channels[i]['announceImage'] so you can call channels[i]['users'], right? But you get a map instead a string. Commented Oct 20, 2021 at 15:32
  • yes I got a map how I can fetch the data like that ? Commented Oct 20, 2021 at 15:49

1 Answer 1

1

You can get the values over here just like any other map containing keys. In your case, users is a map containing keys lastname, name, userid. channels[i]['users'] would get you the users map. To get the value for a particular key ,you can think of channels[i]['users'] as a whole, like an array.

So you could get the values by doing the following :

String lastname = channels[i]['users']['lastname']
String name = channels[i]['users']['name']

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.