Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i concatenate two strings where one needs to be a static variable from a Widget in flutter

Tags:

flutter

dart

I have this code from a stateful widget which looks like

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

How am I supposed to bring the two variables together so that i have something that looks like +1535465345 i am collecting user information

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

also making the phone variable static and printing out the concatenated string brings out +1null

like image 736
Taio Avatar asked Dec 23 '22 05:12

Taio


2 Answers

Instead of having a field, you can have a getter like

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

Refer this answer

like image 165
Dinesh Balasubramanian Avatar answered Dec 25 '22 17:12

Dinesh Balasubramanian


You need to specify the class to access static members

 String finalphone = '${MyClass.code}$phone';
like image 26
Rémi Rousselet Avatar answered Dec 25 '22 17:12

Rémi Rousselet