0

Assume we have a model in our models/ folder:

class ProductModel {
  final String id;
  ProductModel(this.id);
}

and then we create a provider for this model in providers/ folder accordingly:

class ProductProvider with ChangeNotifier {...}

The question is how to make the model being extended in this provider so that we avoid duplication? The solution:

class ProductProvider extends ProductModel with ChangeNotifier {...}

does not work in case if we later create another provider ProductsProvider (plural) for lists.

boldnik
  • 2,547
  • 2
  • 27
  • 32

1 Answers1

1

The Provider-ChangeNotifier Architecture is a bit different than what you show, but you are mostly there.

You should have:

class ProductViewModel with ChangeNotifier {
  final String id;
  ProductModel(this.id);
}

And that's it, now you can pass this ProductViewModel using a ChangeNotifierProvider, as you can see here.

Michel Feinstein
  • 13,416
  • 16
  • 91
  • 173