【问题标题】:Flutter how to get all latitude and longitude from api responseFlutter 如何从 api 响应中获取所有经纬度
【发布时间】:2022-01-11 10:27:39
【问题描述】:

我从 api 得到这样的响应:

[
    {
        "PollId": "5241851",
        "Long": 74.25142,
        "Lat": 31.47104,
        "Speed": 0.0,
        "IsGPSAvailable": true,
        "Status": "IGNITION OFF",
        "Ign": false,
        "Distance": 0.023,
        "Direction": 136.0,
    },
    {
        "PollId": "5255637",
        "Long": 74.25131,
        "Lat": 31.47095,
        "Speed": 0.0,
        "IsGPSAvailable": true,
        "Status": "IGNITION OFF",
        "Ign": false,
        "Distance": 0.028,
        "Direction": 136.0,
    },
]

如何从两者中获取 lat 和 lang。

【问题讨论】:

    标签: flutter api


    【解决方案1】:

    您可以使用this创建模型

    
    // To parse this JSON data, do
    //
    //     final location = locationFromJson(jsonString);
    
    import 'dart:convert';
    
    List<Location> locationFromJson(String str) => List<Location>.from(json.decode(str).map((x) => Location.fromJson(x)));
    
    String locationToJson(List<Location> data) => json.encode(List<dynamic>.from(data.map((x) => x.toJson())));
    
    class Location {
        Location({
            this.pollId,
            this.long,
            this.lat,
            this.speed,
            this.isGpsAvailable,
            this.status,
            this.ign,
            this.distance,
            this.direction,
        });
    
        String pollId;
        double long;
        double lat;
        int speed;
        bool isGpsAvailable;
        String status;
        bool ign;
        double distance;
        int direction;
    
        factory Location.fromJson(Map<String, dynamic> json) => Location(
            pollId: json["PollId"],
            long: json["Long"].toDouble(),
            lat: json["Lat"].toDouble(),
            speed: json["Speed"],
            isGpsAvailable: json["IsGPSAvailable"],
            status: json["Status"],
            ign: json["Ign"],
            distance: json["Distance"].toDouble(),
            direction: json["Direction"],
        );
    
        Map<String, dynamic> toJson() => {
            "PollId": pollId,
            "Long": long,
            "Lat": lat,
            "Speed": speed,
            "IsGPSAvailable": isGpsAvailable,
            "Status": status,
            "Ign": ign,
            "Distance": distance,
            "Direction": direction,
        };
    }
    
    

    然后使用futurebuilder和listview来获取响应

     FutureBuilder(
                      future: _future,
                      builder: (context, AsyncSnapshot<List<Location>> snapshot) {
                        return snapshot.data?.length == 0
                            ? const Center(
                                child: Text("No Item"),
                              )
                            : ListView.builder(
                                shrinkWrap: true,
                                itemBuilder: (context, index) {
                                 final data = snapshot.data[index];
                                 print(data.long, data.lat);
                                 // return UI 
                                },
                                itemCount: snapshot.data?.length ?? 0,
                              );
                      },
                    )
    

    【讨论】:

    • 问题是我不想在 ui 中显示数据。我想将 lat lang 传递给 build 方法之外的列表。
    【解决方案2】:

    Flutter 有大量文档。您可以阅读有关从 Internet 获取数据并处理此数据的所有内容in the documentation

    您的问题的答案在Convert the response into a custom Dart object 部分。他们以专辑为例,但我相信您可以为您的数据结构做到这一点。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-02-28
      • 1970-01-01
      • 2021-05-17
      • 1970-01-01
      • 1970-01-01
      • 2021-09-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多