Search code examples
stringflutterdarttextfielddart-null-safety

A value of type 'int' can't be assigned to a variable of type 'String'


A value of type 'int' can't be assigned to a variable of type 'String'. Try changing the type of the variable, or casting the right-hand type to 'String'.

TextFormField(
                    keyboardType: TextInputType.number,
                    controller: TextEditingController()
                      ..text = '${profileModel.phonenumber}',
                    style: TextStyle(
                      color: Colors.black,
                      fontSize: 17,
                      letterSpacing: 1,
                    ),
                    decoration: InputDecoration(
                      border: OutlineInputBorder(),
                      labelText: "Phone Number",
                      hintText: "enter your phonenumber",
                      floatingLabelBehavior: FloatingLabelBehavior.auto,
                      prefixIcon: Icon(Icons.phone),
                      focusedErrorBorder: OutlineInputBorder(),
                    ),
                    onChanged: (text) {
                      number = text as int;
                      print(number);
                    },
                  ),

CastError (type 'String' is not a subtype of type 'int' in type cast)

onChanged: (text) {
 number = text as int;
 print(number);
},

Solution

  • TextFormField(
         keyboardType: TextInputType.number,
          controller: TextEditingController()
          ..text = '${profileModel.phonenumber}',