'ChangeNotifierProxyProvider 2 positional argument(s) expected in the parameter 'create'
void main() async {
WidgetsFlutterBinding.ensureInitialized();
runApp(
MultiProvider(
providers: [
ChangeNotifierProvider<Auth>(
create: (_) => Auth(),
),
ChangeNotifierProxyProvider<Auth, Products>(
create: (ctx) => Products(),
update: (ctx, value, previousProducts) => Products(value.idToken,
previousProducts == null ? [] : previousProducts.productsList),
),
],
child: MyApp(),
),
);
}
in this line 'create: (ctx) => Products(),' Products() expecte 2 argument and i don't know how to fix this error
where Products class is like this :
class Products with ChangeNotifier {
List<Product> productsList = [];
final String authToken;
Products(this.authToken, this.productsList);
Solution 1:[1]
I think add empty String and list to Products provider ,this resolves your problems. Add like this,
create:(ctx)=> Products('',[]),
Solution 2:[2]
Because your constructor requires 2 arguments, namely authToken and productsList.
You can remove the arguments from the constructor, make them nullable, or assign them default values like this:
class Products with ChangeNotifier {
List<Product> productsList = [];
final String authToken;
Products({this.authToken = "someDefaultValue", this.productsList = const []});
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Suraj Rao |
| Solution 2 | AbdulAziz Umar |


