String to double from a map in Dart

Ive tried to use double.parse, double.tryparse as well as trying to convert it to int but it keeps giving the error ‘type ‘int’ is not a subtype of type ‘String”, ive searched a few sites but all the solutions show to use parse.

final locData = Map<String, dynamic>.from(local);

var dubLat = locData['lat'];

var lat = double.tryParse(dubLat);

ive tried changing var to double nit still give the same error.

>Solution :

The .tryParse takes a string or even the parse method for this instance takes a string. But your data is actually integers therefore the error type int is not a subtype of type string. You should consider either storing them as ints or using the .toString method before parsing them.

var dubLat = locData[‘lat’]; // Here dublat is not actually a string but integer. Your map has dynamic as a second parameter it can take all data types and this particular datatype is int not string which .parse expects.

var lat = double.tryParse(dubLat.toString());

Leave a Reply