How to does constructors in provider works?

Viewed 25
class Brain with ChangeNotifier {
  List<List<IconData>> icon = [];

  Brain() {
    for (int i = 0; i < 10; i++) {
      List<IconData> temp = [];
      for (int j = 0; j < 10; j++) {
        temp.add(Icons.square_outlined);
      }
      icon.add(temp);
    }
    [...]
  }
}

After some testing it seems like provider is calling it's constructor every widget state, but my icon list doesn't reset after changing some of the icons. How is it possible?

1 Answers

Well you need to use notifyListeners(); when you need to refresh your Ui, use it at the end of your Brain constructor after your process finished and see the result

Brain() {
    ...
    notifyListeners();
}

Its better to use a function outside the constructor and use that, the constructor will create once when you provide the provider,

Related