0

i am trying to fetch the data from my firestore collections, that my collection name is messages. but when it running, streambuilder give null value from firebasefirestore stream collection.

firestore stream :

Stream<List<MessageMdl>>? getMyMessage({int? myId}) { try { _firestore .collection('messages') .where('user_one', isEqualTo: myId) .snapshots() .map((event) { var result = event.docs.map<MessageMdl>((e) { return MessageMdl.fromJson(e.data()); }).toList(); result.sort((MessageMdl a, MessageMdl b) => a.createdAt!.compareTo(b.createdAt!)); return result; }); } catch (e) { print(e.toString()); return throw Exception('failed'); } } 

streambuilder :

StreamBuilder<List<MessageMdl>>( builder: (context, AsyncSnapshot snapshot) { print(snapshot.data); if (snapshot.hasError) { return Center(child: Text('Something went wrong')); } if (snapshot.connectionState == ConnectionState.waiting) { return Center(child: Text("Loading")); } if (snapshot.hasData) { return SingleChildScrollView( child: Column( children: snapshot.data!.map((e) { return Row( mainAxisAlignment: MainAxisAlignment.end, children: [ Container( margin: EdgeInsets.only( right: e.userOne == myId ? 20 : 0, top: 10, left: e.userOne == myId ? 0 : 20, ), padding: EdgeInsets.symmetric( horizontal: 20, vertical: 15), constraints: BoxConstraints( maxWidth: MediaQuery.of(context).size.width * .7), decoration: BoxDecoration( color: e.userOne == myId ? Colors.grey.shade200 : Colors.green, borderRadius: BorderRadius.only( topLeft: Radius.circular(12), topRight: Radius.circular(12), bottomLeft: Radius.circular(12))), child: Text(e.message!), ), ], ); }).toList(), ), ); } else { return Center(child: Text('no data')); } }, stream: MessageServices().getMyMessage(myId: myId), ) 

and here is what is snapshot.data value when it get printed in streambuilder : enter image description here

4
  • Can you print result before returning it in getMyMessage service? By the way, you are sorting your result but you don't equal it to result again. Commented Apr 8, 2022 at 23:25
  • it give value, message one, when i call it use print(_result[0].message); Commented Apr 8, 2022 at 23:31
  • when i check in connection state, it give value to else state. not waiting state, error state, or done state. but it goes to else state and give the else return value Commented Apr 8, 2022 at 23:33
  • Did you check here; stackoverflow.com/questions/50542771/… Commented Apr 9, 2022 at 0:16

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.