未处理的例外:处置产品后使用了A产品

时间:2020-01-09 05:53:05

标签: flutter dart

我正在使用MultiProvider,但出现此错误:

Unhandled Exception: A Products was used after being disposed. Once you have called dispose() on a Products, it can no longer be used.

这是我的main.dart文件。这种结构有什么问题?

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(),
          },
        ),
      ),
    );
  }
}

3 个答案:

答案 0 :(得分:2)

使用yourProvider.value 而不是创建您的问题将得到解决。谢谢

  itemBuilder: (ctx, index) => ChangeNotifierProvider.value(
        value: products[index],
        child: ProductItem(),
      ),
      itemCount: products.length,
    );

答案 1 :(得分:0)

我认为您必须将产品提供程序添加到提供程序:[]代理提供程序上方,作为Auth Provider的定义,如下所示:

providers: [
    ChangeNotifierProvider<Auth>.value(
                          value: Auth(),
     ChangeNotifierProvider<Products>.value(
                          value: Products(),
    ChangeNotifierProxyProvider<Auth, Products>(
      update: (ctx, auth, previousProducts) => Products(
        auth.token,
        auth.userId,
        previousProducts == null ? [] : previousProducts.items,
      ),
    ),
    ChangeNotifierProvider<Cart>.value(
      value: Cart(),
    ),
    ChangeNotifierProxyProvider<Auth, Orders>(
      update: (ctx, auth, previousOrders) => Orders(
        auth.token,
        auth.userId,
        previousOrders == null ? [] : previousOrders.orders,
      ),
    ),
  ],

答案 2 :(得分:0)

使用

Navigator.of(context).pushNamed('/');

在您调用 logout 的抽屉中。 这将解决这个问题 谢谢..