【问题标题】:filling a list of objects from json in flutter在颤动中填充来自json的对象列表
【发布时间】:2019-11-22 17:48:01
【问题描述】:

我正在尝试使用静态数据的示例项目,并通过对 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 中实现这一点?

【问题讨论】:

  • 请详细说明您真正想要做什么?你要调用api吗?
  • 是的,我将调用 asp.net 中内置的 webAPI

标签: json flutter dart


【解决方案1】:

要调用 API,

方法一

使用http

像这样调用获取请求,

  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}.");
  }

方法二

使用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 转换为 dart 模型

【讨论】:

  • 我的那部分工作正常,我无法将响应转换为正确的格式。
  • 您是否在解析 json 数据时遇到麻烦?使用 this tool 将您的 json 转换为 dart。然后做类似 MyModel model = new MyModel.fromJson(dataConvertedToJSON);
  • 我成功地将单个对象从 http 返回到 dart,但列表给我带来了麻烦。
  • 所以我可以将多个结果加载到该对象中?那肯定会解决我的问题。
  • 是的,父对象为Class Parent{ List&lt;JsonFormServerModel&gt; list; } 类似这样的东西
猜你喜欢
  • 2018-12-05
  • 2020-04-28
  • 2019-07-13
  • 2020-02-14
  • 1970-01-01
  • 1970-01-01
  • 2015-11-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多