我有一个来自有状态小部件的代码,它看起来像
static String code = '+1';
String phone;
String finalphone = '$code' + '$phone'; =>this declaration brings an error
that 'Only static members can be accessed in initializers'
Run Code Online (Sandbox Code Playgroud)
我应该如何将这两个变量放在一起,以便我看起来像是+1535465345在收集用户信息
//the widget
Widget form() {
return Form(
key: _formKey,
child: TextFormField(
decoration: InputDecoration(
contentPadding: EdgeInsets.all(0.0),
),
style: TextStyle(
letterSpacing: 2.0,
fontSize: 19.0,
fontWeight: FontWeight.bold,
color: Colors.black87),
onSaved: (value) => phone = value, //the (value) here is a
//string which is
//assigned
//to phone variable declared at the top
),
),
);
Run Code Online (Sandbox Code Playgroud)
}
还使电话变量静态并打印出连接的字符串带出 +1null
相反,具有的field,你可以有一个getter像
String get finalphone => '$code' + '$phone';
Run Code Online (Sandbox Code Playgroud)
参考这个答案