Example, I have a Wrap widget, and in children I have some code like:
listTask.map((e) {
if (e.day!.contains(value))
return Padding(
padding: const EdgeInsets.all(4.0),
child: Container(
decoration: BoxDecoration(
shape: BoxShape.circle, color: Colors.black),
height: 20,
width: 20,
),
);
}).toList()
I want it return Padding whenever it pass if statement, but it get error The argument type 'List<Padding?>' can't be assigned to the parameter type 'List<Widget>'
. How can I solve it? Thank you.
2
Answers
The problem is that your callback to
List.map
does not explicitly return anything if theif
condition fails, so the callback implicitly returnsnull
, and you end up with aList<Padding?>
instead of aList<Padding>
.List.map
creates a 1:1 mapping from input elements to output elements.Either filter out the
null
elements afterward or use collection-for
with collection-if
:Also see: How to convert a List<T?> to List in null safe Dart?
Try this :
listTask.map((e) {