dart构造函数copyWith参数显示为null

时间:2019-05-29 18:45:01

标签: flutter dart

我有一个变量title,我用一个字符串值对其进行了初始化。

class NameState{
 List name;
 var title = 'this is a default one'; 
 NameState({this.title,this.name});

 NameState copyWith({title, name}) {

     print(title); // this one should be null as there is no data.
     print(this.title); // null => why this one null as i have title defined


    return new NameState(
        title: title ?? this.title, 
        name: name ?? this.name,
    );
  }
}

这里的title变量有一个值。但是如果我在构造函数中未传递title的任何值,则this.title的值将为null。如何获得this is a default one中的当前值this.title

谢谢。

1 个答案:

答案 0 :(得分:1)

您需要使用此

$userBasicInfo->affiliatedFacilities()->sync($request->get('affiliated_facility', []));

代替

NameState({this.title = 'this is a default one', this.name});