3

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?

notarealgreal
  • 734
  • 16
  • 29

1 Answers1

5

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

Jitesh Mohite
  • 31,138
  • 12
  • 157
  • 147