Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 颤振-更改提供程序包_Flutter_Dart - Fatal编程技术网

Flutter 颤振-更改提供程序包

Flutter 颤振-更改提供程序包,flutter,dart,Flutter,Dart,去年,我一直在使用提供商软件包3.1.0版本在提供商之间共享价值,如下所示: ... return MultiProvider( providers: [ ChangeNotifierProvider.value( value: Auth(), ), ChangeNotifierProxyProvider<Auth, Products>( builder: (ctx, auth, previousProd) => Prod(

去年,我一直在使用提供商软件包3.1.0版本在提供商之间共享价值,如下所示:

...
return MultiProvider(
  providers: [
    ChangeNotifierProvider.value(
      value: Auth(),
    ),
    ChangeNotifierProxyProvider<Auth, Products>(
      builder: (ctx, auth, previousProd) => Prod(
            auth.cred,
            previousProd,
          ),
    ),
    ),
  ],)
...
它表明,
create参数是必需的
,正如我所说的 但我不知道如何使用这个参数

谁能帮帮我我会很感激的,
谢谢

ProxyProvider不需要create参数,但ChangeNotifierProxyProvider需要,以避免每次创建ChangeNotifier(使用ProxyProvider没有问题,因为它是一个没有侦听器的简单类)。create只调用一次,而update可以调用多次,因此ChangeNotifierProxyProvider的代码应该如下所示

 ChangeNotifierProxyProvider<Auth, Products>(
   create: (_) => Prod(),
   update: (_, auth, product) => product..credential = auth.cred,
   //instead of creating a new object Prod(), just reuse the existing one and set the new values
   child: ...
 )
现在,每次Auth更改并通知ProxyProvider时,它都将重用在create中创建的相同对象,只需更改参数credential(_cread)

 ChangeNotifierProxyProvider<Auth, Products>(
   create: (_) => Prod(),
   update: (_, auth, product) => product..credential = auth.cred,
   //instead of creating a new object Prod(), just reuse the existing one and set the new values
   child: ...
 )
class Prod extends ChangeNotifier{
  Credential _cred;

  Prod(){
    //if you want to initialize some values
  }

  set credential(Credential credential) => _cred = credential;
  //or some other logic you do here with the auth.cred
}