0

Here I am trying to get the data from the firebase database using firstore instance in streamBuilder.

  • .collection('/message_data/friendA##friendB/message_list') :-this is my collection-document path from firebase

No error, just a blank page.

     @override
     Widget build(BuildContext context) {
       return Scaffold(
       appBar: AppBar(
       title: Text(widget.friendName),
     ),
     body: Column(
     children: <Widget>[
      Flexible(
          child: StreamBuilder(
        stream: Firestore.instance
            .collection('/message_data/friendA##friendB/message_list')
            .snapshots(),
        builder: (BuildContext context, AsyncSnapshot snapshot) {
          if (snapshot.hasError) {
            return Text('Error on chatView ${snapshot.error.toString()}');
          }
          if (snapshot.connectionState == ConnectionState.active) {
            if (snapshot.hasData) {
              if (snapshot.data.documents .length > 0) {
                return ListView.builder(

                  itemCount: snapshot.data.documents.length,
                  itemBuilder: (BuildContext context, int index) {
                    DocumentSnapshot _document = snapshot.data.documents[index];



                    return ChatMessage(
                      isFriend: _document['fromA'],

                      isNotPrevious:snapshot.data.documents.length - 1 == index,
                      message: _document['content'],
                      friendInitial: 'T',

                      avatarUrl:'https://avatarfiles.alphacoders.com/132/132399.jpg',
                    );
                  },
                );

              }
              else{
                return Text('No messages found in chat view length vala'); 
              }
            }
            else{
                return Text('No messages found in chat view hasdata'); 
              }
          }
          else{
            return CircularProgressIndicator();
          }
        },
      )),
Harsh
  • 83
  • 2
  • 7

1 Answers1

0

I think you have some error in your snapshot, but you also have an error in how you access your firestore data here. You have to access the data property of your DocumentSnapshot in order to access the data.

DocumentSnapshot _document = snapshot.data.documents[index];
Map chatData = _document.data;


return ChatMessage(
    isFriend: chatData['fromA'],

    isNotPrevious:snapshot.data.documents.length - 1 == index,
    message: chatData['content'],
    friendInitial: 'T',

    avatarUrl:'https://avatarfiles.alphacoders.com/132/132399.jpg',
);
dshukertjr
  • 15,244
  • 11
  • 57
  • 94
  • @Harsh I'm sure there is an error with the security rules of firestore. Could you copy and paste it to the question? – dshukertjr Feb 16 '20 at 12:45
  • what do you want me to copy paste to the question? – Harsh Feb 17 '20 at 17:37
  • @Harsh Do you know what security rules are in firestore? I think there's something wrong with it, so I want to see it to fix your issue. Could you go to your firebase console and copy and paste the security rules so that I can fix your issue? – dshukertjr Feb 17 '20 at 23:52
  • rules_version = '2'; service cloud.firestore { match /databases/{database}/documents { match /{document=**} { allow read, write: if true; } } } – Harsh Feb 23 '20 at 12:17