Search code examples
flutterdartuser-interfacereduxviewmodel

StoreConnector doesn't recognise list update as ViewModel Change


I am attempting to get my StoreConnector to recognize an update to a list in my _ViewModel so that my UI will update a list on the screen, I am trying to use the distinct property of the StoreConnector for performance reasons.

Here is an example of the ViewModels I am using, is there a way to generate a list from my store that will be recognized by the StoreConnector?

_GroceryViewModel

class _GroceryViewModel extends Equatable {
  final int amountOfItems;
  final List<String> meats;

  _SmartWearViewModel({
    required this.amountOfItems,
    required this.meats,
  });

  @override
  List<Object?> get props => [
        this.amountOfItems,
        this.meats,
      ];
}

_ViewModel Factory

factory _ViewModel.create(Store<AppState> store) {
    var itemList = store.state.itemState!.meats!
        .map((e) => _GroceryViewModel(
              amountOfItems: e.amountOfItems,
              meats: e.meats,
            ))
        .toList();

    return _ViewModel(
      itemList: itemList,
    );
  }

I have only really tried removing the distinct property and then it works fine if I do that but doesn't help achieve the goal.

I have also tried creating new lists from the list in the store but no luck in that department, curious if there are other ways of making this work?


Solution

  • I found the problem, I was not creating a new List of Strings correctly when making my _GroceryViewModel so it did not register that there was an update to my ViewModel, I added the new keyword before returning my _GroceryViewModel and that seems to have sorted it