You can parse a string into an integer with int.parse(). For example:
var myInt = int.parse('12345');
assert(myInt is int);
print(myInt); // 12345
Note that int.parse() accepts 0x prefixed strings. Otherwise the input is treated as base-10.
You can parse a string into a double with double.parse(). For example:
var myDouble = double.parse('123.45');
assert(myDouble is double);
print(myDouble); // 123.45
parse() will throw FormatException if it cannot parse the input.
You can parse a string into an integer with int.parse(). For example:
var myInt = int.parse('12345');
assert(myInt is int);
print(myInt); // 12345
Note that int.parse() accepts 0x prefixed strings. Otherwise the input is treated as base-10.
You can parse a string into a double with double.parse(). For example:
var myDouble = double.parse('123.45');
assert(myDouble is double);
print(myDouble); // 123.45
parse() will throw FormatException if it cannot parse the input.
In Dart 2 int.tryParse is available.
It returns null for invalid inputs instead of throwing. You can use it like this:
int val = int.tryParse(text) ?? defaultValue;
What are the different methods to convert a string to a double in Flutter?
Why is it necessary to convert strings to doubles in Flutter?
What are some best practices for converting strings to doubles in Flutter?
If the value was parsed from JSON, Dart knows the value is a double and the error is telling me that the value is of type double and the convert expected type string.
So the error is much more clear once I understood!
I was too facing same issue. I know it is a silly answer but it worked for me. I was using Getx to save states and while converting string to double it was displaying error
String is not subtype of type double
as the state of previous variables was being saved, so I restarted the app and tried type casting and it worked well.
Remove the quotes from
var long2 = double.parse('$group1');
to
var long2 = double.parse($group1);
You can also use
var long2 = double.tryParse($group1);
or to also accept numbers without fractions
var long2 = num.tryParse($group1)?.toDouble();
to not get an exception when the string in $group1 can not be converted to a valid double.
Why not use just group1 as in double.parse(group1)