Home > front end >  How to compose a builder in a StreamBuilder in flutter
How to compose a builder in a StreamBuilder in flutter

Time:10-29

I was trying to use the stream builder in flutter but I am getting these errors I do not seem to understand

The argument type 'Type' can't be assigned to the parameter type 'Widget Function(BuildContext, AsyncSnapshot<QuerySnapshot<Object?>>)'.

The body might complete normally, causing 'null' to be returned, but the return type is a potentially non-nullable type.

below is the small code for it

   builder: (BuildContext context , AsyncSnapshot snapshot){
                  if(snapshot.hasData){
                    final messages = snapshot.data.docs;
                    List<Text> messageWidgets = [];
                    for(var message in messages){
                      final messageText = message.data()['text'];
                      final messageSender = message.data()['sender'];
                      final messageWidget = Text('$messageText from $messageSender');
                      messageWidgets.add(messageWidget);
                    }
                    return Column(
                      children: [
                        messageWidgets,
                      ],
                    );
                  }
                },

Please do help me on this

CodePudding user response:

The first line of the error is because of wrong type assigned to Snapshot. And second line error is because you didn't returned anything from the builder body.

Use the StreamBuilder like this,


StreamBuilder<QuerySnapshot<Object?>>(
   stream: your_stream,
   builder: (_, snapshot){
     if(condition){
       return AnyWidget();
     }
     return Something();
   }
)
  • Related