title: Flutter and Distance Matrix API parsing json
body: <p>I am currently building a flutter app where I want to calculate the distance between some objects and am using the Google Distance Matrix API to do so. I am having trouble parsing the json using Dart. All I want ultimately is a list of the distances from the json results so that I can index them and apply them to the data in my app.</p>
  
  <p>The json results look like this:</p>
  
  <pre><code>{
     "destination_addresses" : [
        "destination address",
        "destination address"
     ],
     "origin_addresses" : [ "Origin addresses here" ],
     "rows" : [
        {
           "elements" : [
              {
                 "distance" : {
                    "text" : "4.3 mi",
                    "value" : 6998
                 },
                 "duration" : {
                    "text" : "14 mins",
                    "value" : 848
                 },
                 "status" : "OK"
              },
              {
                 "distance" : {
                    "text" : "6.7 mi",
                    "value" : 10728
                 },
                 "duration" : {
                    "text" : "22 mins",
                    "value" : 1327
                 },
                 "status" : "OK"
              }
           ]
        }
     ],
     "status" : "OK"
  }
  </code></pre>
  
  <p>I ultimately would like to end up with a list (in dart) that is just a list of the distance "text" values within the elements array but I am having trouble getting this to return. I have tried creating a class and mapping the json results to this but unsuccesfully as I am not very good at parsing json so any advice on how to end up with this list would be gratefully received!</p>
  
  <p>I have tried this code to parse the json but am really struggling to make it work and then apply it:</p>
  
  <pre><code>class Topleveladd {
    final String elements;
  
    Topleveladd({this.elements});
  
    factory Topleveladd.fromJson(Map&lt;String, dynamic&gt; parsedJson) {
      return Topleveladd(elements: parsedJson['rows']);
    }
  }
  
  class Elements {
    List&lt;Distance&gt; distanceslist;
  
    Elements({this.distanceslist});
  
    factory Elements.fromJson(Map&lt;String, dynamic&gt; parsedJson) {
      var list = parsedJson['elements'] as List;
      print(list.runtimeType); //returns List&lt;dynamic&gt;
      List&lt;Distance&gt; distancesList =
          list.map((i) =&gt; Distance.fromJson(i)).toList();
      return Elements(distanceslist: distancesList);
    }
  }
  
  class Distance {
    String text;
    Distance({this.text});
  
    factory Distance.fromJson(Map&lt;String, dynamic&gt; parsedJson) {
      return new Distance(
        text: parsedJson['distance'],
      );
    }
  }
  </code></pre>