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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/azure/11.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
仅当构造函数dart中不为空时更新最终字段_Dart_Dart Pub - Fatal编程技术网

仅当构造函数dart中不为空时更新最终字段

仅当构造函数dart中不为空时更新最终字段,dart,dart-pub,Dart,Dart Pub,我有一个名为App的类,其中包含一些字段。 我只想在传递给构造函数的参数不为null时初始化final字段 e、 在下面的代码中,我只想在config参数不为null时设置它 我尝试了下面的代码,但它不工作 @immutable class App { final AuthState auth; final RConfig config; App({AuthState auth, this.config}): auth = auth ?? new Aut

我有一个名为App的类,其中包含一些字段。 我只想在传递给构造函数的参数不为null时初始化final字段

e、 在下面的代码中,我只想在config参数不为null时设置它

我尝试了下面的代码,但它不工作

@immutable
class App {
    final AuthState auth;
    final RConfig config;

    App({AuthState auth, this.config}):
        auth = auth ?? new AuthState(), config = config != null ? config : this.config ;
虽然我对dart很陌生,但我知道我不能两次初始化final字段,但有一些应用程序状态会导致再次创建AppState,此时RConfig可能为null


我的问题很简单,就是如何在分配字段之前检查多个最终字段的有效性,例如null或empty。

一个字段不能初始化两次。您可以使用初始化形式(
this.config
)和初始值设定项列表条目
config=…
来初始化
config

我不确定您想用
config
做什么,但首先将其声明为一个普通参数,然后确定您想做什么:

AppState({AuthState auth, RConfig config})
    : auth = auth ?? new AuthState(), config = config; // or something

不能两次初始化字段。您可以使用初始化形式(
this.config
)和初始值设定项列表条目
config=…
来初始化
config

我不确定您想用
config
做什么,但首先将其声明为一个普通参数,然后确定您想做什么:

AppState({AuthState auth, RConfig config})
    : auth = auth ?? new AuthState(), config = config; // or something

我通常不熟悉可选注释,它们对程序的行为没有影响(它们可以让工具给你额外的警告,但程序的含义不会改变)。我不认为
不可变的
注释有任何区别-我上面给出的代码是有效的,如果您需要其他东西,您需要弄清楚它是什么。或者为可选参数定义默认值更容易:
AppState({AuthState auth=AuthState.dummy(),RConfig config}{…}
我通常不熟悉可选注释,它们对程序的行为没有影响(它们可以让工具给你额外的警告,但程序的含义不会改变)。我不认为
不可变的
注释有任何区别-我上面给出的代码是有效的,如果您需要其他东西,您需要弄清楚它是什么。或者为可选参数定义默认值更容易:
AppState({AuthState auth=AuthState.dummy(),RConfig config}{…