Home > Back-end >  Flutter: Another exception was thrown: FormatException: Invalid double on '-' double
Flutter: Another exception was thrown: FormatException: Invalid double on '-' double

Time:06-27

TextFormField(
   decoration: InputDecoration(
     labelText: 'Coordination - Latitude',
   ),
   inputFormatters: [
     FilteringTextInputFormatter.allow(
         RegExp(r'^(\-*\d )?\.?\d{0,4}'))
   ],
   onChanged: (v) {
     lat(double.parse(v));
   },
 ),

I have this value for the lat value. v is a string and when I add - then I get Another exception was thrown: FormatException: Invalid double error. How can I parse value with the - like -23.4348?

CodePudding user response:

The reason is -(minus) alone will not be considered as num, so you are getting exceptions. But when you enter a number and then add a minus sign, you won't face the issue. So add conditions like this

    onChanged: (v) {
      if(v == '-' || v.isEmpty) {
        return;
      } else {
        lat(double.parse(v));
      }
    }

Note: Your regex may need some tweaks.

  • Related