flutter
flutter

Reputation: 6786

null value in ListBody

I've a method that returns a List of Widgets.

 List<Widget> _children(List<Suburb> suburbs){
      return
        suburbs.map((suburb){
          Text(suburb.name);
        }).toList()
      ;

  }

I'm using this method for the children in the show showDialog()method:

                       showDialog(
                        context: context,
                        builder: (BuildContext context) {
                          return SimpleDialog(
                            children: _children(suburbs), //But this works [Text(suburbs[0].name)]
                            backgroundColor: Colors.white,
                            title: Text('Pick your suburb'),
                          );
                        }),

But I'm getting the error ListBody's children must not contain any null values, but a null value was found at index 0But this works: [Text(suburbs[0].name)] Any idea why this is happening...

Upvotes: 0

Views: 86

Answers (1)

Josteve Adekanbi
Josteve Adekanbi

Reputation: 12733

You didn't return the Text in your map

Add a return just before the Text

Like this

 List<Widget> _children(List<Suburb> suburbs){
      return
        suburbs.map((suburb){
          return Text(suburb.name);
        }).toList()
      ;
  }

Or you can use the arrow function like this

 List<Widget> _children(List<Suburb> suburbs){
      return suburbs.map((suburb) => Text(suburb.name)).toList();
  }

Upvotes: 1

Related Questions