Flutter DistanceMatrix JSON无法正确解析 [英] Flutter DistanceMatrix JSON won't parse correctly

查看:110
本文介绍了Flutter DistanceMatrix JSON无法正确解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已阅读并尝试了所有帖子/视频,但没有任何效果。请帮助对代码感兴趣的网络管理员。

I've read and tried every post/vid but nothing is working. Please help a network admin interested in code.

我不知道如何将返回的json对象获取到可用的可用列表/字符串中。我只需要Elements-> Distance-> Text&

I can't figure out how I get the json object that is returned, to a usable list/string that I can use. I just need Elements -> Distance -> Text & value to a Text widget.

返回的JSON对象

{
   "destination_addresses" : [ "The destination address" ],
   "origin_addresses" : [ "The origin address" ],
   "rows" : [
      {
         "elements" : [
            {
               "distance" : {
                  "text" : "7 m",
                  "value" : 7
               },
               "duration" : {
                  "text" : "1 min.",
                  "value" : 1
               },
               "status" : "OK"
            }
         ]
      }
   ],
   "status" : "OK"
}

代码段(删除了先前的尝试):

Code snippet (deleted previous tries):

Future<String> getLinkForCalculation() async{
    var _latUser = "INSERT LATUSER";
    var _lonUser = "INSERT LONUSER";
    var _latClient = "INSERT LATCLIENT";
    var _lonClient = "INSERT LONCLIENT";
    var _key = "INSERT_API_KEY";
    var link = "https://maps.googleapis.com/maps/api/distancematrix/json?units=metric&origins=$_latUser,$_lonUser&destinations=$_latClient,$_lonClient&key=$_key";
    print(link);
    return link;
  }


  Future<List> calculateDistanceFromLink()async{
    var link = await getLinkForCalculation();
    Dio finalLink = new Dio();
    Response distanceData = await finalLink.get(link);

    print(distanceData.data);

  }

我已阅读并尝试过:

  • Flutter and Distance Matrix API parsing json (Implemented the class, imported the method loadData() but my app is giving errors all over the place)
  • https://medium.com/flutterdevs/parsing-complex-json-in-flutter-b7f991611d3e (Implemented and altered the class to work with my data. Not working again.)
  • Unhandled Exception: InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'List<dynamic>
  • How to Deserialize a list of objects from json in flutter
  • Flutter: Get data from a list of json

推荐答案

我不是Flutter开发人员,所以我无法为您提供文本小部件的帮助。但我已经举了一个示例,说明如何从JSON中获取值:

I am not a Flutter developer so I cannot help you with the Text widget part. But I have made this example of how you can get the value from your JSON:

import 'dart:convert';

class Element {
  final String text;
  final int value;

  Element.fromJson(dynamic json)
      : text = json['text'] as String,
        value = json['value'] as int;

  @override
  String toString() => 'text: $text\nvalue: $value';
}

void main() {
  const jsonString = '''
  {
   "destination_addresses" : [ "The destination address" ],
   "origin_addresses" : [ "The origin address" ],
   "rows" : [
      {
         "elements" : [
            {
               "distance" : {
                  "text" : "7 m",
                  "value" : 7
               },
               "duration" : {
                  "text" : "1 min.",
                  "value" : 1
               },
               "status" : "OK"
            }
         ]
      }
   ],
   "status" : "OK"
}
''';

  final dynamic jsonObj = json.decode(jsonString);

  final list = Element.fromJson(jsonObj['rows'][0]['elements'][0]['distance']);
  print(list);
}

如您所见,如果数据类型是动态的,那真的很容易浏览JSON以从中获取特定数据。这样做的副作用是类型安全性已从分析仪中消失了,因此您仅应在较小的隔离区域中使用动态功能,例如解析然后返回普通类型的安全对象,并在其余的代码中使用它们。

As you can see, if the data type is dynamic, it is really easy to browse through the JSON to get specific data from it. The side effect of this is that type safety is gone from the analyzer so you should only use the power of dynamic in small isolated areas e.g. parsing and then return normal type safe objects and use them in the rest of you code.

这篇关于Flutter DistanceMatrix JSON无法正确解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆