Flutter how to update Listview.builder if the data is changes
3,022
If it's continuously changing then use StreamBuilder
Eg: https://bendyworks.com/blog/a-month-of-flutter-rendering-a-list-view-with-stream-builder
If not then you can use FutureBuilder
Eg: https://medium.com/nonstopio/flutter-future-builder-with-list-view-builder-d7212314e8c9
OR,
If dataList gets change inside application(remove/update), then call setState()
which will update ListView
Author by
notarealgreal
Updated on December 24, 2022Comments
-
notarealgreal over 1 year
I'm working with a ListView.builder in Flutter. For now all was working fine, I have the follwing lines of code in the callback methods:
@override void initState(){ items = new List(); for(int index = 0; index < dataList.length; index++){ items.add(...creating widget ..); } } @override Widget build(BuildContext context) { return Container( child: ListView.builder( scrollDirection: Axis.vertical, shrinkWrap: true, itemCount: dataList.length, itemBuilder: (BuildContext context, int index) { return Container( width: width, height: 160 , key: UniqueKey(), padding: EdgeInsets.all(10), child: items[index], ); }, ), ); }
The problem is when the data of the dataList is updated, how should I implement this list to make the list update the UI if the data values are updated, removed or place changed. Is StreamBuilder the solution?
-
Jitesh Mohite over 3 yearsin your case it is dataList right?
-
notarealgreal over 3 years@jitsm555 yes, sorry!
-
-
notarealgreal over 3 yearsIs continously changing but I solved this problem with a ChangeNotifier in the class definition of dataList items. The problem is when this items are removed or index changed. Which option I should use?
-
Jitesh Mohite over 3 yearshow they are removing or modifying? its done inside app or on server?
-
notarealgreal over 3 yearsInside app, depending on user interaction.
-
Jitesh Mohite over 3 yearsso whenever dataList gets modified you just have to call setState(), it will update the listview