1

Do you think it's okay to have a list of StateNotifiers inside the State of other StateNotifier? For example like this:

class ListOfItems extends StateNotifier<ListState> {
  void doSomethingWithList() {
    // update state of list -> rebuild ListWidget
  }

  void doSomethingWithItem(int itemIndex) {
    final item = state.items[itemIndex];
    item.doSomethingWithItem();
  }
}

class ListState {
  final List<Item> items;
  // some other properties

  ListState(this.items);
}

class Item extends StateNotifier<ItemState> {
  // can be called directly from the ItemWidget (on a button press) or from the ListOfItems
  void doSomethingWithItem() {
    // update state of item -> rebuild ItemWidget
  }
}

class ItemState {
  // some properties
}

I tried it and it seems to me that it works well. Could there be pitfalls?

1 Answers1

0

This might look a little strange based on SOLID principles. However, it will work fine. The important thing is not to change on "someone else's" state outside the class.

Ruble
  • 2,589
  • 3
  • 6
  • 29