首页 > 解决方案 > 如何从 Flutter 中的 Dio 请求中获取所有字段

问题描述

我正在使用 Dio 从这个 Api 获取请求:Api Example

响应如下:

{
"count": 87, 
"next": "https://swapi.co/api/people/?page=2", 
"previous": null, 
"results": [
    {
        "name": "Luke Skywalker", 
        "height": "172", 
        "mass": "77", 
        "hair_color": "blond", 
        "skin_color": "fair", 
        "eye_color": "blue", 
        "birth_year": "19BBY", 
        "gender": "male", 
        "homeworld": "https://swapi.co/api/planets/1/", 
        "films": [
            "https://swapi.co/api/films/2/", 
            "https://swapi.co/api/films/6/", 
            "https://swapi.co/api/films/3/", 
            "https://swapi.co/api/films/1/", 
            "https://swapi.co/api/films/7/"
        ], 
        "species": [
            "https://swapi.co/api/species/1/"
        ], 
        "vehicles": [
            "https://swapi.co/api/vehicles/14/", 
            "https://swapi.co/api/vehicles/30/"
        ], 
        "starships": [
            "https://swapi.co/api/starships/12/", 
            "https://swapi.co/api/starships/22/"
        ], 
        "created": "2014-12-09T13:50:51.644000Z", 
        "edited": "2014-12-20T21:17:56.891000Z", 
        "url": "https://swapi.co/api/people/1/"
    }, 

我在飞镖中有这个代码:

 void _getNames() async {
final response = await dio.get('https://swapi.co/api/people');

List tempList = new List();
for (int i = 0; i < response.data['results'].length; i++) {
  tempList.add(response.data['results'][i]);
}
setState(() {
  names = tempList;
  names.shuffle();
  filteredNames = names;
});

}

使用此代码,我只能获得结果的名称,但我不知道如何获得其他字段,我尝试了一些东西但没有任何效果。我知道这很容易,但我不知道该怎么做。

标签: jsonhttpdartflutterrequest

解决方案


干得好

 _getPeople() async {
    var response = await http.get('https://swapi.co/api/people/');
    if (response != null && response.statusCode == 200) {
      ResultModel jsonResponse =
          ResultModel.fromJson(convert.jsonDecode(response.body));
      print(jsonResponse);
    }
  }

结果模型代码是

class ResultModel {
  int count;
  String next;
  dynamic previous;
  List<Result> results;

  ResultModel({
    this.count,
    this.next,
    this.previous,
    this.results,
  });

  factory ResultModel.fromJson(Map<String, dynamic> json) {
    return ResultModel(
      count: json['count'],
      next: json['next'],
      previous: json['previous'],
      results: _parseResult(json['results']),
    );
  }
}

_parseResult(List<dynamic> data) {
  List<Result> results = new List<Result>();
  data.forEach((item) {
    results.add(Result.fromJson(item));
  });
  return results;
}

_parseString(List<dynamic> data) {
  List<String> results = new List<String>();
  data.forEach((item) {
    results.add(item);
  });
  return results;
}

class Result {
  String name;
  String height;
  String mass;
  String hairColor;
  String skinColor;
  String eyeColor;
  String birthYear;
  String gender;
  String homeworld;
  List<String> films;
  List<String> species;
  List<String> vehicles;
  List<String> starships;
  String created;
  String edited;
  String url;

  Result({
    this.name,
    this.height,
    this.mass,
    this.hairColor,
    this.skinColor,
    this.eyeColor,
    this.birthYear,
    this.gender,
    this.homeworld,
    this.films,
    this.species,
    this.vehicles,
    this.starships,
    this.created,
    this.edited,
    this.url,
  });

  factory Result.fromJson(Map<String, dynamic> json) {
    return Result(
      name: json['name'],
      height: json['height'],
      mass: json['mass'],
      hairColor: json['hairColor'],
      skinColor: json['skinColor'],
      eyeColor: json['eyeColor'],
      birthYear: json['birthYear'],
      gender: json['gender'],
      homeworld: json['homeworld'],
      films: _parseString(json['films']),
      species: _parseString(json['species']),
      vehicles: _parseString(json['vehicles']),
      created: json['created'],
      edited: json['edited'],
      url: json['url'],
    );
  }
}

推荐阅读