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
setState
on 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
setState
calls within the item widget, this has much less performance impact than callingsetState
on 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?
2
Answers
Stateless
ListView
builder items are efficient as they can be quicklyreused on a large list, but any changes to any item require calling
setState
on the wholeListView
widget.Stateful
ListView
builder items allow updating each item viasetState
calls within the item widget, which has less performance impact than
calling
setState
on the wholeListView
widget, but each row has itsown 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 smallerListView
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 theListView.builder
to reuse them.if your ListView items are simple and don’t have any internal state or interactivity, using Stateless widgets would be more performant and easier to manage. If the items have internal state, interactivity, or more complex behavior, using Stateful widgets would be a better choice.
Keep in mind that Flutter is optimized for fast UI updates, so the performance impact of Stateful vs Stateless ListView.builder items is not significant, especially for a small to medium-sized list of items. It is always recommended to profile your application and check performance metrics.
Also do not forget about inherited widgets
Inherited widgets provide a way to pass data down the widget tree without needing to manually pass it through every level. Inherited widgets can be useful when you want to share a common piece of information or state across multiple widgets without having to rebuild the entire widget tree when that information changes.
Here is an example
Wrap your ListView.builder with your custom InheritedWidget, passing the necessary data and methods:
Access the data from the InheritedWidget in your ListView.builder’s item:
In this example, we’re using an InheritedWidget to manage and share a counter value across all ListView.builder items. Each item can access the counter and the increment method without having to pass them explicitly through constructors.
Inherited widgets work well with both Stateless and Stateful ListView.builder items, depending on your specific use case. They can help you manage shared state across multiple widgets without needing to rebuild the entire tree when the data changes.
There really is no correct answer to this. You pretty much have to do what is best for your usecase.