Not able to access the data from firebase using StreamBuilder in flutter

时光总嘲笑我的痴心妄想 提交于 2020-03-05 03:13:42

问题


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();
          }
        },
      )),

回答1:


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',
);


来源:https://stackoverflow.com/questions/60142173/not-able-to-access-the-data-from-firebase-using-streambuilder-in-flutter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!