How to build dynamic lists of widgets?

This is one of the most common issues faced by Flutter developers.

Indeed, the List literal is not easy to work with and creating a complex list can be a painful experience — be it conditionally adding a widget in a Column or concatenating a children argument with custom logic.

As such, it is not uncommon to find code that looks like this:

Do you have trouble reading this example? Don’t worry, me too.

This horror is due to a combination of facts:

Dart does not have a spread operator

Widgets do not allow null as a valid child

as a valid child Widgets are supposed to be entirely immutable. So building the list in multiple steps doesn’t feel right.

These facts led to “Allow null values in child or children[] collections” be one of the most upvotes issues on Flutter’s GitHub.