0

I'm using Provider to do some filtering in my application. I'm using a ChangeNotifier for each of my filters. The filters themselves have children, but all updates are going through the ChangeNotifier class.

Here is my code:

    MultiProvider(
      providers: [
        ChangeNotifierProvider(
          create: (context) => Seasons(seasons: [], localizedName: "Season"),
        ),
        ChangeNotifierProxyProvider<Seasons, GarmentFilterProvider>(
          create: (context) {
            print('Creating!');
            return GarmentFilterProvider(seasons: null);
          },
          update: (context, seasons, garmentFilterProvider) {
            print('updating!');
            return garmentFilterProvider!..update(seasons);
          },
        ),
      ],
      child: const ClothesIndex(),
    ),

Both Creating and Updating are never printed. Even though inside Seasons I have the following (you can ignore the super() call, it's an abstraction layer for filters):

  Seasons({required this.seasons, required this.localizedName})
      : super(
            fieldName: backendFieldname,
            localizedName: localizedName,
            filterOptions: seasons) {
    getLoadedIntoProvider();
  }

  void getLoadedIntoProvider() async {
    seasons = await getFromBackend();
    notifyListeners();
  }

Seasons inherits an abstract class like so:

abstract class Filter extends ChangeNotifier {
  String fieldName;
  String localizedName;
  List<FilterOption> filterOptions;
  bool isExpanded;

  Filter({
    required this.fieldName,
    required this.localizedName,
    required this.filterOptions,
    this.isExpanded = false,
  });
}

Now I'm curious why my ChangeNotifierProxyProvider never creates, nor updates. Does anybody know why this is happening?

Danoctum
  • 321
  • 5
  • 16

1 Answers1

0

Found the problem.

I wasn't consuming the GarmentFilterProvider anywhere yet. That's why it didn't initialize and didn't update.

I've added a Consumer<GarmentFilterProvider> to my ClothesIndex and it works perfectly now! Leaving this question and answer in case someone else has the same 'issue'.

Danoctum
  • 321
  • 5
  • 16