0

Withvar pinU = int.parse(pin.text); I get this error:

E/flutter (16045): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: FormatException: Invalid number (at character 1)
E/flutter (16045):
E/flutter (16045): ^

With var pinU = pin as int; I get this error:

E/flutter (16045): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: type 'String' is not a subtype of type 'int' in type cast

I'm trying to pass a PIN to the database helper function to get the results. Here is my complete function:

login2() async {
    var usernameU = username.text;
    var pinU = int.parse(pin.text);

    await DBProvider.db.getUser(usernameU, pinU).then((tempUser) {
      Navigator.push(context as BuildContext,
          MaterialPageRoute(builder: (context) => const WelcomePage()));
    }).catchError((err) {
      // ignore: avoid_print
      print('Error: $err');
    });
  }

I need to pass a int, but this error is persistent.

jamesdlin
  • 81,374
  • 13
  • 159
  • 204

2 Answers2

0

Instead of var use int as datatype.

int pinU = int.parse(pin.text ?? "0");
Fahmida
  • 1,050
  • 8
  • 19
  • with your solution I'm getting this error: 'The left operand can't be null, so the right operand is never executed. Try removing the operator and the right operand.' – Andres Rodriguez Mar 20 '23 at 11:08
0

Try this:

if (pin.text.isEmpty) {
   // tell a user that this field is required or do something else
   return;
}

final pinU = int.parse(pin.text);
   

You also need to set your TextField to accept only numbers, or you should use tryParse instead.

Andrei Volgin
  • 40,755
  • 6
  • 49
  • 58