0

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);
},
Md. Yeasin Sheikh
  • 54,221
  • 7
  • 29
  • 56

3 Answers3

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

pmatatias
  • 3,491
  • 3
  • 10
  • 30
0

You can write like this :

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

need to change number = text as int; to number = int.parse(text);

Hardik Mehta
  • 2,195
  • 1
  • 11
  • 14
  • ChromeProxyService: Failed to evaluate expression '_primaryCompleter': InternalError: No frame with index 14. – Tariq Jamal A Aug 01 '22 at 16:25
  • Seeems to be flutter version related issue : check this : https://stackoverflow.com/questions/71521564/chromeproxyservice-failed-to-evaluate-expression-handleprimarypointerinterna – Hardik Mehta Aug 02 '22 at 04:14
0

I will recommend using int.tryParse. Because it can fail on parser too when the input text will not number. If number is not nullable, provide a default value.

number = int.tryParse(text)?? 0;

And if number is string just do

  number = text;
Md. Yeasin Sheikh
  • 54,221
  • 7
  • 29
  • 56