发生变化时在ChangeNotifierProxyProvider中更新的构建器

时间:2020-07-16 11:41:23

标签: flutter dart builder flutter-provider

首先感谢您检查我的问题。

我是一个初学者,我有以下代码:

ChangeNotifierProxyProvider<Auth, ImgProvider>(
          builder: (ctx, auth, previousItem) => ImgProvider(
            auth.token,
            auth.userId,
            previousItem == null ? [] : previousItem.items,
          ),
        ),

????????p

ChangeNotifierProxyProvider

?????????????。 ????:

ChangeNotifierProxyProvider<Auth, ImgProvider>(
          update: (ctx, auth, previousItem) => ImgProvider(
            auth.token,
            auth.userId,
            previousItem == null ? [] : previousItem.items,
          ),

??????????????'??????'????????。”

??“”:“???“????????”。

1 个答案:

答案 0 :(得分:0)

解决方案是像这样更改它:

 ChangeNotifierProxyProvider<Auth, ImgProvider>(
          create: (ctx) => ImgProvider(),
          update: (ctx, auth, previousItem) => previousItem
            ..receiveToken(
                auth, previousItem == null ? [] : previousItem.items),
        ), 

并在ImgProvider()中创建一个函数,如下所示:

void receiveToken(Auth auth, List<ImgVar> items) {
    authToken = auth.token;
    userId = auth.userId;
    _items = items;
  }