gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 06:35:53 24 4
gpt4 key购买 nike

我从一个有状态的小部件中获得了这段代码,它看起来像

 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
),
),
);
}

同时使 phone 变量成为静态变量并打印出连接的字符串带出 +1null

最佳答案

您可以使用 getter 而不是 field

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

引用this回答

关于dart - 我如何连接两个字符串,其中一个字符串需要是 flutter 中 Widget 的静态变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51823468/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com