簡體   English   中英

Flutter 如何從 api 響應中獲取所有經緯度

[英]Flutter how to get all latitude and longitude from api response

我從 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。

您可以使用創建 model


// 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,
                          );
                  },
                )

Flutter 有大量文檔。 您可以在文檔中閱讀有關從 Internet 獲取數據和處理這些數據的所有信息

您的問題的答案在將響應轉換為自定義 Dart object 部分 他們以專輯為例,但我相信你可以為你的數據結構做到這一點。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM