Note that this is for ListView.builder items, not general stateless vs stateful widget discussion.
Stateless listview builder item:
- Pros
- I assume each item can be quickly reused on a large list as they don’t contain any state
- Cons
- Any changes to any item would require to call
setStateon list view
- Any changes to any item would require to call
Stateful listview builder item:
- Pros
- As recommended in Performance best practices, we can update each item via
setStatecalls within the item widget, this has much less performance impact than callingsetStateon the whole list view widget.
- As recommended in Performance best practices, we can update each item via
- Cons
- As each row now has its own state, I guess they cannot be reused?
So which one would you recommend?
>Solution :
-
Stateless
ListViewbuilder items are efficient as they can be quickly
reused on a large list, but any changes to any item require calling
setStateon the wholeListViewwidget. -
Stateful
ListViewbuilder items allow updating each item viasetState
calls within the item widget, which has less performance impact than
callingsetStateon the wholeListViewwidget, but each row has its
own state and cannot be reused.
Recommendation:
Use the approach that best fits your specific use case and performance requirements. If you need to update individual items frequently and the ListView is large, consider using stateful items. If you have a smaller ListView and don’t need to update individual items frequently, stateless items may be sufficient.
You can try giving a unique Key to each Stateful item in the ListView.builder to reuse them.