简体   繁体   中英

Flutter how to get all latitude and longitude from api response

Im getting respone from api like this:

[
    {
        "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,
    },
]

how to get lat and lang from both.

You can create a model using 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,
    };
}

and then use a futurebuilder and listview to get the response

 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 has an extensive documentation. You can read all about fetching data from the internet and handling this data in the documentation .

The answer to your question is in the part Convert the response into a custom Dart object . They do it using an Album as an example, but I'm sure you can do it for your data structure.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM