当我在 flutter 的 didChangeDependencies 中调用多个提供者时,它显示错误
when I call more than one provider in didChangeDependencies in flutter ,it shows me error
当我在 didChangeDependencies 中调用多个提供程序时。它向我显示此错误“A Products was used after being disposed,Once you have called dispose() on a Products, it can not no longer be used.”
这是我在 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(),
),
],
这是我在 homescreen.dart 中的 didchangeddependencies。
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);});
}
}
当我在 didChangeDependencies 中调用多个提供程序时。它向我显示此错误“A Products was used after being disposed,Once you have called dispose() on a Products, it can not no longer be used.”
这是我在 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(),
),
],
这是我在 homescreen.dart 中的 didchangeddependencies。
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);});
}
}