从扑中的JSON填充对象列表

时间:2019-11-22 17:48:01

标签: json flutter dart

我正在尝试使用静态数据作为示例项目,并通过对flutter中的端点的http调用使其动态化。

编辑--- JSON代码

Future<User> getUsers(String groupID ) async {

  final response =
  await http.post('http://url/api/GetUsers',
  headers: {"Content-Type": "application/json", 
             'Accept': 'application/json',},
  body: json.encode({'groupID' : groupID}));

  if (response.statusCode == 200) {
   // If the call to the server was successful, parse the JSON
    User _users;
   _users = User.fromJson(json.decode(response.body));
    return _users;

数据的格式是这样的:

List<Pokemon> _pokemonList = [
  Pokemon(id: 1, name: 'Bill Smith',type: 'Caregiver', image: 'assets/images/KC_logo.png'),
  Pokemon(id: 2, name: 'Joy Smith',type: 'Caregiver', image: 'assets/images/pokimon_2.png'),
  Pokemon(id: 3, name: 'Joe Smith',type: 'Member', image: 'assets/images/pokimon_3.png'),
  Pokemon(id: 4, name: 'Pete Smith',type: 'Member', image: 'assets/images/pokimon_4.png'),
  Pokemon(id: 5, name: 'Steve Smith',type: 'Caregiver', image: 'assets/images/pokimon_5.png'),
  Pokemon(id: 6, name: 'Paul Smith',type: 'Member', image: 'assets/images/pokimon_6.png'),
  Pokemon(id: 7, name: 'Balbasaur',type: 'Grass', image: 'assets/images/pokimon_7.png'),
  Pokemon(id: 8, name: 'Balbasaur',type: 'Water', image: 'assets/images/pokimon_8.png'),
  Pokemon(id: 9, name: 'Balbasaur',type: 'Rock', image: 'assets/images/pokimon_9.png'),
  Pokemon(id: 11, name: 'Charlizard',type: 'Fire', image: 'assets/images/pokimon_11.png'),
  Pokemon(id: 12, name: 'Charlizard',type: 'Rock', image: 'assets/images/pokimon_12.png'),
  Pokemon(id: 13, name: 'Charlizard',type: 'Grass', image: 'assets/images/pokimon_13.png'),
  Pokemon(id: 14, name: 'Charlizard',type: 'Water', image: 'assets/images/pokimon_14.png'),
  Pokemon(id: 15, name: 'Charlizard',type: 'Fire', image: 'assets/images/pokimon_15.png'),
  Pokemon(id: 16, name: 'Charlizard',type: 'Grass', image: 'assets/images/pokimon_16.png'),
  Pokemon(id: 17, name: 'Charlizard',type: 'Fire', image: 'assets/images/pokimon_17.png'),
  Pokemon(id: 18, name: 'Charlizard',type: 'Water', image: 'assets/images/pokimon_18.png'),
  Pokemon(id: 19, name: 'Charlizard',type: 'Rock', image: 'assets/images/pokimon_19.png'),
  Pokemon(id: 20, name: 'Charlizard',type: 'Fire', image: 'assets/images/pokimon_20.png'),
  Pokemon(id: 21, name: 'Charlizard',type: 'FiWaterre', image: 'assets/images/pokimon_21.png'),
  Pokemon(id: 22, name: 'Charlizard',type: 'Grass', image: 'assets/images/pokimon_22.png'),
  Pokemon(id: 23, name: 'Charlizard',type: 'Rock', image: 'assets/images/pokimon_23.png'),
  Pokemon(id: 24, name: 'Charlizard',type: 'Grass', image: 'assets/images/pokimon_24.png'),
  Pokemon(id: 25, name: 'Pichu',type: 'Water', image: 'assets/images/pokimon_25.png'),
  Pokemon(id: 26, name: 'Charmender',type: 'Fire', image: 'assets/images/pokimon_26.png'),
  Pokemon(id: 27, name: 'Charlizard',type: 'Rock', image: 'assets/images/pokimon_27.png'),
 ];
 }

如何在Dart中实现?

1 个答案:

答案 0 :(得分:0)

要调用API,

方法1

  

使用http

调用这样的get请求,

  var url = "https://www.your_api_url.com?query=a";

  // Await the http get response, then decode the json-formatted responce.
  var response = await http.get(url);
  if (response.statusCode == 200) {
    var jsonResponse = convert.jsonDecode(response.body);
    var itemCount = jsonResponse['totalItems'];
    print("Number of books about http: $itemCount.");
  } else {
    print("Request failed with status: ${response.statusCode}.");
  }

方法2

  

使用Dio

获取示例请求

import 'package:dio/dio.dart';
void getHttp() async {
  try {
    Response response = await Dio().get("http://www.your_api_url.com");
    print(response);
  } catch (e) {
    print(e);
  }
}

使用this tool

将json转换为飞镖模型