Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Stateful vs Stateless widgets for ListView.builder items

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

Stateful listview builder item:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  • 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 calling setState on the whole list view widget.
  • Cons
    • As each row now has its own state, I guess they cannot be reused?

So which one would you recommend?

>Solution :

  • Stateless ListView builder items are efficient as they can be quickly
    reused on a large list, but any changes to any item require calling
    setState on the whole ListView widget.

  • Stateful ListView builder items allow updating each item via setState
    calls within the item widget, which has less performance impact than
    calling setState on the whole ListView widget, 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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading