skip to Main Content

in flutter i have a listView that gets it’s item from a list and the list is downloded from the internet, whenever a new item added to the list i have an event that tells the app a new item added, and for that i use notifyListners() to refresh the list view to display the new item,
my problem with this method is that sometimes there are big sized items in the list and refreshing all of them the moment a new item comes it really slows down the app and it’s such a bad user experience, what i want to do is get the list one time and only notify the app about the new item

enter image description here

2

Answers


  1. ListView.builder(
        itemCount: items.length,
        itemBuilder: (context, index) {
          return ListTile(
            title: Text(items[index]),
            // You can add more customization here
          );
        },
      ),
    )
    

    Instead of list view you can use ListView.builder.

    ListView.builder is a convenient widget for efficiently creating a scrollable list of widgets. It only creates widgets for the items that are currently in view, which makes it efficient for large lists.

    Login or Signup to reply.
  2. You can only update a newly added item

    class ItemProvider with ChangeNotifier {
     List<Item> _items = [];
    
     void addItem(Item newItem) {
       _items.add(newItem);
       notifyListeners();
     }
    
     List<Item> getItems() {
       return _items;
     }
    }
    

    In list view only itemwidget will get rebuild instead of all the list

    ListView.builder(
     itemCount: provider.getItems().length,
     itemBuilder: (context, index) {
       return ItemWidget(item: provider.getItems()[index]);
     },
    );
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search