Converting a string to double in Dart using double.parse()

11,200

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!

Share:
11,200
Rinktacular
Author by

Rinktacular

Gamer. Coder. I primarily work with Angular/Django Rest Framework backend.

Updated on June 04, 2022

Comments

  • Rinktacular
    Rinktacular almost 2 years

    I am trying to convert coordinates returned from the Google Geo. API into doubles. A simplified part of the response that gets returned is...

    { 
      locationData: {
        bounds: {
          northeast: 40.222222,
          southwest: 38.265987
        }
      }
    }
    

    When I use print(locationData['bounds']['northeast']) my console reads that and understands that the value is 40.222222 However, when I try to use a parse method, I get the following error:

    var neLat = double.parse(locationData['bounds']['northeast']);

    I/flutter (10537): type 'double' is not a subtype of type 'String' in type cast where

    I/flutter (10537): double is from dart:core

    I/flutter (10537): String is from dart:core

    That error leads me to believe that my parse is not valid, even though it is clearly a double value, just as a string. Am I missing something for this conversion? I have seen conversion in multiple examples using double.parse, I just can't figure out what my issue is..

  • Nicholas Jela
    Nicholas Jela almost 6 years
    So how to solve this? I am using dart, I parse a double from JSON and sigh it into a string like: "${this.myDoubleValue}" but got that error.
  • Rinktacular
    Rinktacular almost 6 years
    @NicholasJela My problem was that I was trying to convert using double.parse on my value, but Dart had already coverted my JSON into a double, so it was trying to parse a double into a double, which is what was throwing the error.