We can use ListView.builder
for lists. However, I found another way using Column
.
Here are two sample codes:
ListView.builder
List list = ["A", "B", "C"];
…
ListView.builder(
itemCount: list.length,
itemBuilder: (context, index) => Text(list[index]),
),
Column
List list = ["A", "B", "C"];
…
Column(children: list.map((element) => Text(element)).toList())
So, my question is, which code should I use?
Feel free to leave a comment if you need more information.
2
Answers
Use base on you need.
Short answer:
You can read this documentation: https://docs.flutter.dev/cookbook/lists/long-lists
Of course they are different.
Column
doesn’t support scroll function. Then you need to wrap withSingleChildScrollView
but also there are some pros and cons.Also I recommend my article here where I explain more about it: https://medium.com/easyread/my-october-flutter-notes-2-6e0c78cf2e56
ListView.builder
is useful when you have a large number of items that can change dynamically, as it only builds thechildren
that are currently visible. It also allows you to easily scroll through the list of items.Column
, on the other hand, is a layout widget that arranges itschildren
vertically. It’s useful when you have a small number ofchildren
that you want to stack vertically and the height of thechildren
is known.Here are a few guidelines to help you decide:
you want to allow the user to scroll through them, use
ListView.builder
.to stack vertically and the height of the
children
is known, useColumn
.children
and you want to stack them vertically, you can useColumn
with
ListView
as child. It’s also possible to use bothListView.builder
andColumn
together, depending on the requirement.It’s always best to experiment with both and decide which one works best for your specific layout and use case.