I'm using MultiProvider
and I get this error:
Unhandled Exception: A Products was used after being disposed. Once you have called dispose() on a Products, it can no longer be used.
Here is my main.dart file. What is wrong with this structure?
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MultiProvider(
providers: [
ChangeNotifierProvider.value(
value: Auth(),
),
ChangeNotifierProxyProvider<Auth, Products>(
update: (ctx, auth, previousProducts) => Products(
auth.token,
auth.userId,
previousProducts == null ? [] : previousProducts.items,
),
),
ChangeNotifierProvider.value(
value: Cart(),
),
ChangeNotifierProxyProvider<Auth, Orders>(
update: (ctx, auth, previousOrders) => Orders(
auth.token,
auth.userId,
previousOrders == null ? [] : previousOrders.orders,
),
),
],
child: Consumer<Auth>(
builder: (ctx, auth, _) => MaterialApp(
title: 'MyShop',
theme: ThemeData(
primarySwatch: Colors.purple,
accentColor: Colors.deepOrange,
fontFamily: 'Lato',
),
home: auth.isAuth
? ProductOverviewScreen()
: FutureBuilder(
future: auth.tryAutoLogin(),
builder: (ctx, authResultSnapshot) =>
authResultSnapshot.connectionState ==
ConnectionState.waiting
? SplashScreen()
: AuthScreen(),
),
routes: {
ProductDetailScreen.routeName: (ctx) => ProductDetailScreen(),
CartScreen.routeName: (ctx) => CartScreen(),
OrdersScreen.routeName: (ctx) => OrdersScreen(),
UserProductsScreen.routeName: (ctx) => UserProductsScreen(),
EditProductsScreen.routeName: (ctx) => EditProductsScreen(),
},
),
),
);
}
}
Remember to dispose of the TextEditingController when it is no longer needed. This will ensure we discard any resources used by the object. This example creates a TextField with a TextEditingController whose change listener forces the entered text to be lower case and keeps the cursor at the end of the input.
To dispose provider controller and delete it in flutter You can Just Destroy Provider Listener by setting listen: false in your Provider Just use the below line: provider. of(context). (listen: false) Now, Your provider controller will be disposed of.
use yourProvider.value Instead of Create Your Problem will Be Solved . thanks
itemBuilder: (ctx, index) => ChangeNotifierProvider.value(
value: products[index],
child: ProductItem(),
),
itemCount: products.length,
);
I was also getting the same error and I resolved it by:
ChangeNotifierProvider<"Provider name">.value(value:<PassedValue>);
It seems ChangeNotifierProvider creates an instance of the provider and when its work is done dispose() method is called and therefore, that instance is cleared with ChangeNotifierProvider.value, that's not the case and we are able to re-use the earlier created instance.
Here are the official docs: https://pub.dev/packages/provider
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With