[Solved] FormatException: Invalid double Error in Flutter

In this post, we are going to show you how to solve the "FormatException: Invalid double" error that occurred on Flutter or Dart. This error occurs when you are doing double.parse() with the invalid source of the string.

══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞══════
The following FormatException was thrown building Builder:
Invalid double

This error occurs, when you are putting an invalid string format to double.parse().

To solve this error, you can extract the double numbers from the string using Regex, and then double.parse() it. For Example:

String str = "Hafce00453dfg23.4fdksd";
double newdbl = double.parse(str.replaceAll(RegExp(r'[^0-9.]'),''));
print(newdbl);// output: 45323.4

//if there is no dot
String str1 = "Hafce00453dfg234fdksd";
double newdbl1 = double.parse(str1.replaceAll(RegExp(r'[^0-9.]'),''));
print(newdbl1);// output: 453234.0

Here, we extract the double from the string and then parse it to double. 

In this way, you can solve "FormatException: Invalid double" error in Flutter or Dart. 

No any Comments on this Article


Please Wait...