with the latest updates I got this:
warning: The parameter 'update' is required. . (missing_required_param
at [pos_mobile] lib\main.dart)
return MultiProvider(
providers: [
ChangeNotifierProvider.value(
value: Auth(),
),
ChangeNotifierProxyProvider<Auth, Operatori>( // here I got the warning
builder: (ctx, auth, prevData) => Operatori(auth.token, auth.userId,
prevData == null ? [] : prevData.operatori),
),
thanks
Check the ChangeNotifierProxyProvider doc
It should be :
ChangeNotifierProxyProvider<Foo, MyChangeNotifier>(
create: (_) => MyChangeNotifier(),
update: (_, foo, myNotifier) => myNotifier
..foo = foo,
child: ...
);
Related
I'm writing an app using BLOC architecture and registered bloc providers like this in the main.dart:
runApp(MultiBlocProvider(providers: [
BlocProvider<OrderBloc>(
create: (context) {
return OrderBloc()..add(OrderInitialEvent());
},
),
BlocProvider<AuthenticationBloc>(
create: (context) {
return AuthenticationBloc(userService: userService)..add(AppStarted());
},
),
...
], child: MyApp()));
Now I need to use Provider approach along with BLOC but not sure how to register it? Is it possible? Thanks
You can just nest them:
runApp(MultiBlocProvider(providers: [
BlocProvider<OrderBloc>(
create: (context) {
return OrderBloc()..add(OrderInitialEvent());
},
),
BlocProvider<AuthenticationBloc>(
create: (context) {
return AuthenticationBloc(userService: userService)..add(AppStarted());
},
),
...
],
child:
MultiProvider(
providers: [
Provider<Something>(create: (_) => Something()),
Provider<SomethingElse>(create: (_) => SomethingElse()),
Provider<AnotherThing>(create: (_) => AnotherThing()),
],
child: MyApp(),
)));
When I call more than one provider in didChangeDependencies .It shows me this error" A Products was used after being disposed,Once you have called dispose() on a Products, it can no longer be used."
It's my providers in main.dart.
MultiProvider(
providers: [
ChangeNotifierProvider(
create: (context) => Auth(),
),
ChangeNotifierProxyProvider<Auth, Products>(
create: (_) => Products(null),
update: (ctx, auth, previousproduct) => Products(auth.userid)
// previousproduct == null ? [] : previousproduct.items),
),
ChangeNotifierProxyProvider<Auth, AddressData>(
// create: (context) => AddressData(null,[]),
update: (Context, auth, previousAddresses) => AddressData(auth.userid,
previousAddresses == null ? [] : previousAddresses.Addresses),
),
ChangeNotifierProxyProvider<Auth, Orders>(
// create: (context) => Orders(null, []),
update: (ctx, auth, previousorders) => Orders(
auth.userid, previousorders == null ? [] : previousorders.orders),
),
ChangeNotifierProvider(
create: (context) => Cart(),
),
],
It's my didchangedependencies in homescreen.dart.
void didChangeDependencies() {
final prod = Provider.of<Products>(context, listen: false);
// TODO: implement didChangeDependencies
prod.fetchandsetproduct();
prod.fetchcategoriesproduct();
final prod2 = Provider.of<Auth>(context,listen: false);
prod2.getcurrentuser();
super.didChangeDependencies();
}
You can call any number of provider
AboutusBlock aboutusBlock;
Loader loader;
HomeBlock homeBlock;
InitialBlock initialBlock;
#override
void didChangeDependencies() {
super.didChangeDependencies();
final aboutusBlock = Provider.of<AboutusBlock>(context);
final loader = Provider.of<Loader>(context);
final homeBlock = Provider.of<HomeBlock>(context);
final initialBlock = Provider.of<InitialBlock>(context);
if (this.aboutusBlock != aboutusBlock ||
this.loader != loader ||
this.homeBlock != homeBlock ||
this.initialBlock != initialBlock) {
this.loader = loader;
this.aboutusBlock = aboutusBlock;
this.homeBlock = homeBlock;
this.initialBlock = initialBlock;
Future.microtask((){
homeBlock.getHomeConfig(loader, initialBlock);
aboutusBlock.getAboutUsConfig(loader);});
}
}
I'm trying to use Multiple providers in my application, but i'm facing some compile time error at builder by using below code
Code
ChangeNotifierProvider(builder: (_) => FirstProvider()),
ChangeNotifierProvider(builder: (_) => SecondProvider()),
you can use MultiProvider to do so. as following.
MultiProvider(
providers: [
ChangeNotifierProvider(builder: (_) => FirstProvider()),
ChangeNotifierProvider(builder: (_) => SecondProvider()),
],
child: someWidget,
)
Hey builder was deprecated you can refer this link for more info Builder Deprecation
We can use Multiple providers like this
MultiProvider(
providers: [
ChangeNotifierProvider<ProductDataProvider>(
create: (_) => ProductDataProvider()),
ChangeNotifierProvider<AuthenticationProvider>(
create: (_) => AuthenticationProvider()),
],
child: Container())
You can use it like this:
MultiProvider(
providers: [
ChangeNotifierProvider(create: (context) => CartModel()),
Provider(create: (context) => SomeOtherClass()),
],
child: MyApp(),
),
MultiProvider(
providers: [
ChangeNotifierProvider(
create: (context) => AuthProvider(authService: AuthService()),
),
GraphQLProvider(...),
],
)
The element type GraphQLProvider can't be assigned to the list type SingleChildWidget.
How to fix this issue? I tried to add create and How do I pass these parameters?
ProxyProvider<YelloChatDb, UserDao>(
builder: (context, yelloChatDb, userdAO) => UserDao(yelloChatDb),
),
ProxyProvider<YelloVendorClient, VendorService>(
builder: (context, yelloVendorClient, categoryService) =>
VendorService.create(yelloVendorClient.chopperClient),
dispose: (context, categoryService) => categoryService.client.dispose()),
Dao class
#UseDao(tables: [Users])
class UserDao extends DatabaseAccessor<YelloChatDb> with _$UserDaoMixin {
UserDao(YelloChatDb db) : super(db);
...
}
Change log 3.2.0 https://pub.dev/packages/provider#320
Deprecated "builder" of providers in favor to "create"
You can use create instead of builder
example from https://pub.dev/packages/provider#proxyprovider
code snippet
Widget build(BuildContext context) {
return MultiProvider(
providers: [
ChangeNotifierProvider(create: (_) => Counter()),
ProxyProvider<Counter, Translations>(
create: (_, counter, __) => Translations(counter.value),
),
],
child: Foo(),
);
}