首页 > 解决方案 > 我如何连接两个字符串,其中一个字符串需要是颤动中小部件的静态变量

问题描述

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

 static String code = '+1';
 String phone;
 String finalphone = '$code' + '$phone';  =>this declaration brings an error 
 that 'Only static members can be accessed in initializers'

我应该如何将这两个变量放在一起,以便我有一些看起来像是+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
    ),
  ),
);
}

还使电话变量静态并打印出连接的字符串带出+1null

标签: dartflutter

解决方案


field你可以有一个getter赞,而不是一个

String get finalphone => '$code' + '$phone';

参考这个答案


推荐阅读