0

I have a list of objects that inherit from ChangeNotifier. I have removed the types just to simplify stuff.

class TaskListsState with ChangeNotifier {
  List _lists = List();
  _currentList;

  List get lists => _lists;
  get currentList => _currentList;

  set lists(List newValue) {
    this._lists = newValue;
    notifyListeners();
  }

  set currentList(newValue) {
    this._currentList = newValue;
    notifyListeners();
  }

  TaskListsState() {
    this._lists = [
      ToDoListState(),
      ToCreateListState(),
      ToDecideListState(),
      BalancedMeListState()
    ];

    this._currentList = this._lists[0];
  }
}

All of the objects inherit from a base class but generally, this is how they look

class ToCreateListState with ChangeNotifier implements TaskListState {
  String title = "TO CREATE LIST";
  String alias = "tocreate";
  bool _loading = false;
  List<TaskState> _tasks;

  bool get loading => _loading;
  List<TaskState> get tasks => _tasks;

  set tasks(List<TaskState> newValue) {
    this._tasks = newValue;
    notifyListeners();
  }

  set loading(bool newValue) {
    _loading = newValue;
    notifyListeners();
  }

  ToCreateListState();

  removeTask(TaskState task) {
    this._tasks.remove(task);
    notifyListeners();
  }
}

Now, when I update one of the list objects, they don't update in the UI. How can I fix this, please?

Currently, this is how I am using it in the view:

 MultiProvider(
      providers: [
        ChangeNotifierProvider(
          create: (context) => TaskListsState(),
        ),

      ],
      child: MaterialApp(
        title: '',
        debugShowCheckedModeBanner: false,
        theme: ThemeData(
          primarySwatch: Colors.blue,
          fontFamily: 'Montserrat',
          visualDensity: VisualDensity.adaptivePlatformDensity,
        ),
        initialRoute: defaultHome,
        onGenerateRoute: onGenerateRoute,
      ),
    );

Then when I want to use one of the items in my screen I would do Provider.of<TaskListsState>(context).lists[index].whateverValue =. anotherValue;

However, it doesn't update.

NduJay
  • 760
  • 1
  • 10
  • 23

2 Answers2

2

When you run the Provider.of<TaskListsState>(context).lists[index].whateverValue =. anotherValue; it won't call notifyListeners() because it doesn't update the lists itself.

You can simply create a method inside the TaskListsState that does the modification for you and afterwards calls the notifyListeners()

class TaskListsState with ChangeNotifier {
...
    void updateWhateverValue(int index, dynamic value) {
        lists[index].whateverValue = value;
        notifyListeners();
    }
...
}

You should replace the dynamic with the object you have.

Gabber235
  • 168
  • 2
  • 8
0

Here is another explanation: Imagine we have this class

class Products with ChangeNotifier
{

List<String> prods =new List() :

List<String> getProds => prods;

Void addprod() 
{
prods.add('chocolate');
prods.add('beer');
notifyListeners();
} 

Products()
{
addprod();
}


} 

So we have a class products which has already two products.

Case of removing a product, if we call

Provider.of<Products>(context)().products.remove('beer')

Beer will get removed but all other Consumers will not get Notified, since remove is a predefined function and doesn't exist on Products class as a Notifier.

Make sure everytime you are calling a function, it's Actually updating something in your data model class, so you can notice changes.

Hope that clarify your vision.

Amir Bennasr
  • 216
  • 1
  • 2
  • 10
  • 1
    This doesn't work for nested objects. Thanks for trying though but the youtube video provided in the first answer worked for me – NduJay Jun 06 '20 at 10:17