Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unhandled Exception: A Products was used after being disposed

Tags:

flutter

dart

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(),
          },
        ),
      ),
    );
  }
}
like image 657
Javid A. Avatar asked Jan 09 '20 05:01

Javid A.


People also ask

Do I need to dispose TextEditingController?

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.

How do I dispose of provider flutter?

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.


2 Answers

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,
    );
like image 115
Tasnuva Tavasum oshin Avatar answered Jan 01 '23 12:01

Tasnuva Tavasum oshin


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

like image 35
abhi Avatar answered Jan 01 '23 12:01

abhi