skip to Main Content
[!Here is the code where i got an error, I could not use [] for this code

return ListView.builder(
          reverse: true,
          itemCount: chatDocs?.length,
          itemBuilder: (ctx, index) => MessageBubble(
              chatDocs[index].data()['text'],
              chatDocs[index].data()['username'],
              chatDocs[index].data()['userImage'],
              chatDocs[index].data()['userId'] == user.uid,


          ),
          // Container(
          //   padding: const EdgeInsets.all(8),
          //    child:  Text(chatDocs?[index]['text']),)
        );

I also tried null check and ‘as Map’ to chat-Docs, But both are didnot work

3

Answers


  1. add .toList() in chatDocs declaration line
    so it becomes like this:

    final chatDocs = chatSnapshot.data?.docs.toList();
    
    Login or Signup to reply.
  2. .map returns an Iterable which doesn’t have the [] operator. You can solve this by turning it into a list by calling toList() on it. So

    final chatDocs = chatSnapshot.data?.docs.map(docs).toList();
    

    But it looks like your map() is already wrong. Note the error there. Do you really need to map it? If not then this already should work

    final chatDocs = chatSnapshot.data?.docs;
    

    Or actually this to handle it in case it’s null

    final chatDocs = chatSnapshot.data?.docs ?? [];
    
    Login or Signup to reply.
  3. You can use this way,

    StreamBuilder(
              stream: _yoursteam,
              builder: ((context, AsyncSnapshot snapshot) {
                return snapshot.hasData
                    ? Padding(
                        padding: const EdgeInsets.symmetric(vertical: 5),
                        child: ListView.builder(
                          itemCount: snapshot.data.docs.length,
                          itemBuilder: (context, index) {
                            return MsgTile(
                               snapshot.data.docs[index]["msgId"],
                               snapshot.data.docs[index]["time"],
                              snapshot.data.docs[index]["message"],
                              snapshot.data.docs[index]["sender"],
                              FirebaseAuth.instance.currentUser!.uid ==
                                  snapshot.data.docs[index]["uid"],
                            );
                          },
                        ),
                      )
                    : Container();
              }),
            );
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search