I'm new to Flutter and, currently build an app which has a list with a series of different item types.
Since the amount of item is unknown and, due to its variety, the ListView.builder
constructor seemed the best option for me at the time, since I could create a Widget
for each item and return them properly on itemBuilder
in a vey clean manner. However, I didn't find a nice way to implement dividers with ListView.builder
and ended up changing to ListView.separated
. The change was effortless and only required passing a new separatorBuilder
parameter.
The problem is, Flutter docs state that ListView.separated
"is appropriate for list views with a fixed number of children".
My questions are:
- Why
ListView.separated
is not suitable for a large number of items whileListView.builder
is? - Is there any performance issues with
ListView.separated
for a high quantity of items? - Would there be a better way to implement dividers that depends on items' types using
ListView.builder
that wouldn't involve me wrapping myWidgets
in aPadding
or something like that?
CodePudding user response:
ListView.separated
is the same as ListView.builder
, the point is that it uses the builder function to build the widgets same as ListView.builder
.
doc link doc
However if it's ListView.separated
, ListView.builder
or any other widget that uses the builder
should have specific number of children in case using lists because if it doesn't know how much items there is you would get out of range exception.
CodePudding user response:
Answering your questions:
1. Why ListView.separated is not suitable for a large number of items while ListView.builder is?
The docs also say:
This constructor is appropriate for list views with a large number of item and separator children because the builders are only called for the children that are actually visible.
So, according to the page you read and the page I quoted here, in both, the builder is called only once for each item in the list. No matter the size of the list. It only mentions, the list should be of fixed size, not small size. It's due to a range exception that could occur.
2. Is there any performance issues with ListView.separated for a high quantity of items?
As stated before, the performance isn't much different, since the builder is called the same amount of time.
3. Would there be a better way to implement dividers that depends on items' types using ListView.builder that wouldn't involve me wrapping my Widgets in a Padding or something like that?
There is no need to, since ListView.separated
is perfect for your case.