In flutter, widgets such as Row
/ ListView
/ Stack
don't handle null children. So if we want to conditionally add widgets as children I usually do the following:
Row(
children: <Widget>[
foo == 42 ? Text("foo") : Container(),
],
);
But this feels weird to add an empty container.
Another solution is a where
filter :
Row(
children: <Widget>[
foo == 42 ? Text("foo") : null,
].where((t) => t != null).toList(),
);
This solves the empty container problem but we still have an ugly ternary and it is tiresome to write.
Is there any better solution?
In flutter, widgets such as Row
/ ListView
/ Stack
don't handle null children. So if we want to conditionally add widgets as children I usually do the following:
Row(
children: <Widget>[
foo == 42 ? Text("foo") : Container(),
],
);
But this feels weird to add an empty container.
Another solution is a where
filter :
Row(
children: <Widget>[
foo == 42 ? Text("foo") : null,
].where((t) => t != null).toList(),
);
This solves the empty container problem but we still have an ugly ternary and it is tiresome to write.
Is there any better solution?
In flutter, widgets such as Row
/ ListView
/ Stack
don't handle null children. So if we want to conditionally add widgets as children I usually do the following:
Row(
children: <Widget>[
foo == 42 ? Text("foo") : Container(),
],
);
But this feels weird to add an empty container.
Another solution is a where
filter :
Row(
children: <Widget>[
foo == 42 ? Text("foo") : null,
].where((t) => t != null).toList(),
);
This solves the empty container problem but we still have an ugly ternary and it is tiresome to write.
Is there any better solution?
In flutter, widgets such as Row
/ ListView
/ Stack
don't handle null children. So if we want to conditionally add widgets as children I usually do the following:
Row(
children: <Widget>[
foo == 42 ? Text("foo") : Container(),
],
);
But this feels weird to add an empty container.
Another solution is a where
filter :
Row(
children: <Widget>[
foo == 42 ? Text("foo") : null,
].where((t) => t != null).toList(),
);
This solves the empty container problem but we still have an ugly ternary and it is tiresome to write.
Is there any better solution?
Row( children: [ if (_id == 0) ...[ Container() ] else if(_id == 1)...[ Text("Hello") ] else ...[ SizedBox(width: 20) ], ], ),
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.