Home > Software engineering >  Why does my widget's build method think I am returning NULL?
Why does my widget's build method think I am returning NULL?

Time:04-28

I don't understand why this is giving an error. All of my if/else paths will always return something, but the code seems to think there is a path that ends in NULL?

The error:

The following _TypeError was thrown building: type 'Null' is not a subtype of type 'Widget'

@override
  Widget build(BuildContext context) {
    final messageDao = MessageDao(groupIDPath: widget.groupIDPath);
    var msg;
    return Scaffold(
      body: Container(
        child: ListView.builder(
          itemCount: data?.length ?? 0,
          itemBuilder: (context, index) {
            return data?.forEach((key, value) { //ERROR POINTS HERE

              msg = Message.fromJson(value);

              dataList?.add(msg);

              if (dataList?[index] == null) { 
                return Text('dataList[index] is NULL');
              } else {
                var receivedType = dataList?[index].type.toString();
                var receivedUid = dataList?[index].uid.toString();
                var receivedTimestamp = dataList?[index].timestamp.toString();
                var receivedText = dataList?[index].text.toString();
                var receivedFilterID = dataList?[index].filterID.toString();
                var receivedM2URL = dataList?[index].m2URL.toString();

                if (dataList?[index].type == '1') { //message type 1
                  return messageDao.m1Widget(receivedType!, receivedUid!, receivedTimestamp!, receivedText!); 
                } else if (dataList?[index].type == '2') { //message type 2
                  return messageDao.m2Widget(receivedType!, receivedUid!, receivedTimestamp!, receivedFilterID!, receivedM2URL!);
                } else {
                  return Text('UNKNOWN MESSAGE TYPE!');
                }
              }
            });
          }
        ),
      )
    );
  }

CodePudding user response:

for each void function it returns nothing. https://api.dart.dev/stable/2.16.2/dart-core/Iterable/forEach.html

CodePudding user response:

Get your data as List

Add this line before returning the data

var dataList = data as List<Modal>; // use your chat data modal inside list

full code

@override
  Widget build(BuildContext context) {
    final messageDao = MessageDao(groupIDPath: widget.groupIDPath);
    var msg;
    return Scaffold(
      body: Container(
        child: ListView.builder(
          itemCount: data?.length ?? 0,
          itemBuilder: (context, index) {
var dataList = data as List<Modal>; // use your chat data modal inside list
            return data?.forEach((key, value) { //ERROR POINTS HERE

              msg = Message.fromJson(value);

              dataList?.add(msg);

              if (dataList?[index] == null) { 
                return Text('dataList[index] is NULL');
              } else {
                var receivedType = dataList[index].type.toString();
                var receivedUid = dataList[index].uid.toString();
                var receivedTimestamp = dataList[index].timestamp.toString();
                var receivedText = dataList[index].text.toString();
                var receivedFilterID = dataList[index].filterID.toString();
                var receivedM2URL = dataList[index].m2URL.toString();

                if (dataList?[index].type == '1') { //message type 1
                  return messageDao.m1Widget(receivedType!, receivedUid!, receivedTimestamp!, receivedText!); 
                } else if (dataList?[index].type == '2') { //message type 2
                  return messageDao.m2Widget(receivedType!, receivedUid!, receivedTimestamp!, receivedFilterID!, receivedM2URL!);
                } else {
                  return Text('UNKNOWN MESSAGE TYPE!');
                }
              }
            });
          }
        ),
      )
    );
  }

  • Related