Keep a list from provider up to date with AnimatedListView
Asked Answered
D

1

9

I'm using the Provider library and have a class that extends ChangeNotifier. The class provides an UnmodifiableListView of historicalRosters to my widgets.

I want to create an AnimatedListView that displays these rosters. The animated list supposedly needs a separate list which will tell the widget when the widget to animate in or out new list items.

final GlobalKey<AnimatedListState> _listKey = GlobalKey<AnimatedListState>();

This example uses a model to update both the list attached to the widget's state and the AnimateListState at the same time.

// Inside model
void insert(int index, E item) {
  _items.insert(index, item);
  _animatedList.insertItem(index);
}

I want to do the same thing but I'm having trouble thinking how. That is, what's a good way I can update the list provided by provider and also change the AnimatedListState.

Diagnose answered 2/7, 2019 at 17:12 Comment(0)
C
0

when you call updateListAndAnimate from your widget or elsewhere, it should ensure that both the list provided by Provider and the AnimatedListState are updated simultaneously.

import 'package:flutter/foundation.dart';

class YourChangeNotifierClass extends ChangeNotifier { List historicalRosters = []; // Your data list

// AnimatedListState key final GlobalKey _listKey = GlobalKey();

// Method to update both the list and AnimatedListState void updateListAndAnimate(YourItemType newItem) { int index = historicalRosters.length;

// Update the data list
historicalRosters.add(newItem);

// Notify listeners to rebuild widgets using the updated list
notifyListeners();

// Update the AnimatedListState
_listKey.currentState?.insertItem(index);

} }

Chainman answered 5/2 at 10:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.