Home > Net >  The body might complete normally, causing 'null' to be returned, but the return type
The body might complete normally, causing 'null' to be returned, but the return type

Time:07-16

The body might complete normally, causing 'null' to be returned, but the return type, 'Widget', is a potentially non-nullable type. Try adding either a return or a throw statement at the end.

Widget buildSingleMessage(Message message) {
    FutureBuilder<List<Message>>(
      future: fetchResult(),
      builder: (context, snapshot) {
        if (snapshot.hasError ||
            snapshot.data == null ||
            snapshot.connectionState == ConnectionState.waiting) {
          return CircularProgressIndicator();
        }
        return Container(
          alignment: message.sender.id == widget.receiver
              ? Alignment.centerLeft
              : Alignment.centerRight,
          padding: EdgeInsets.all(10.0),
          margin: EdgeInsets.all(10.0),
          child: Text(message.message),
        );
      },
    );
  }

CodePudding user response:

Your function must return widget. For now buildSingleMessage only call FutureBuilder but return nothing. so you should add return before FutureBuilder

Widget buildSingleMessage(Message message) {
   return FutureBuilder<List<Message>>(
      future: fetchResult(),
      builder: (context, snapshot) {
        if (snapshot.hasError ||
            snapshot.data == null ||
            snapshot.connectionState == ConnectionState.waiting) {
          return CircularProgressIndicator();
        }
        return Container(
          alignment: message.sender.id == widget.receiver
              ? Alignment.centerLeft
              : Alignment.centerRight,
          padding: EdgeInsets.all(10.0),
          margin: EdgeInsets.all(10.0),
          child: Text(message.message),
        );
      },
    );
  }

or fat arrow

Widget buildSingleMessage(Message message) =>
    FutureBuilder<List<Message>>(
      future: fetchResult(),
      builder: (context, snapshot) {
        if (snapshot.hasError ||
            snapshot.data == null ||
            snapshot.connectionState == ConnectionState.waiting) {
          return CircularProgressIndicator();
        }
        return Container(
          alignment: message.sender.id == widget.receiver
              ? Alignment.centerLeft
              : Alignment.centerRight,
          padding: EdgeInsets.all(10.0),
          margin: EdgeInsets.all(10.0),
          child: Text(message.message),
        );
      },
    );
  • Related