Home > Software design >  String to double from a map in Dart
String to double from a map in Dart

Time:01-26

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.

CodePudding user response:

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());

CodePudding user response:

You are facing this error because you are trying to pass int but double.tryParse need string value that's why getting this error 'type 'int' is not a subtype of type 'String'

I solved by this 'double.tryParse(dubLat.toString());'

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

      var dubLat = locData['lat'];

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