我如何连接两个字符串,其中一个字符串需要是颤振中 Widget 的静态变量

Tai*_*aio 3 dart flutter

我有一个来自有状态小部件的代码,它看起来像

 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

Rém*_*let 6

需要指定类来访问静态成员

 String finalphone = '${MyClass.code}$phone';
Run Code Online (Sandbox Code Playgroud)


Din*_*ian 5

相反,具有的field,你可以有一个getter

String get finalphone => '$code' + '$phone';
Run Code Online (Sandbox Code Playgroud)

参考这个答案