如何在Flutter中将“ChangeNotifierProxyProvider”升级到新版本?

3lxsmp7m  于 2022-12-30  发布在  Flutter
关注(0)|答案(1)|浏览(144)

我得到了这个代码:

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

但它似乎已被弃用,并在hangeNotifierProxyProvider行中出现以下错误:

The parameter 'create' is required.
The parameter 'update' is required.

我不知道如何升级这部分代码?

eqqqjvef

eqqqjvef1#

我尝试了下面的代码,似乎它的工作,但我仍然不确定我做的对吗?

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

相关问题