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

Share:
3,022
notarealgreal
Author by

notarealgreal

Updated on December 24, 2022

Comments

  • notarealgreal
    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
      Jitesh Mohite over 3 years
      in your case it is dataList right?
    • notarealgreal
      notarealgreal over 3 years
      @jitsm555 yes, sorry!
  • notarealgreal
    notarealgreal over 3 years
    Is 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
    Jitesh Mohite over 3 years
    how they are removing or modifying? its done inside app or on server?
  • notarealgreal
    notarealgreal over 3 years
    Inside app, depending on user interaction.
  • Jitesh Mohite
    Jitesh Mohite over 3 years
    so whenever dataList gets modified you just have to call setState(), it will update the listview