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:
You’ve likely heard of Flutter. And you probably know how powerful widgets are. But they have an issue: they require a good amount of boilerplate.
Let’s analyze our typical widget:
11 lines for a simple
Text with arguments is a lot! Due to using classes, we have many duplicates and unnecessary bits of code:
super(key: key)are redundant. We never want a widget without these.
Functions would be a perfect fit. It makes the syntax much shorter:
Almost perfect right?
Well, no. Functions have…
A software engineer passionated in Flutter. You’ll find me most of the time on stackoverflow answering Flutter related questions.