在颤振中从 Web 获取 JSON

时间:2020-12-21 11:09:26

标签: json flutter dart

我昨天开始使用 flutter 并尝试在列表视图中从网络获取 json 数据(我是初学者)。

我为我的 json 数据创建了一个类:

class Events {
  final int id;
  final int categorie_id;
  final String categorie;
  final DateTime begin;
  final DateTime end;
  final String title;
  final String description;
  final String ort;
  final String fileurl;
  final int canceled;

  Events({this.categorie_id, this.categorie, this.begin, this.end, this.description, this.ort, this.fileurl, this.canceled, this.id, this.title});

  factory Events.fromJson(Map<String, dynamic> json) {
    return Events(
      id: json['id'],
      categorie_id: json['categorie_id'],
      categorie: json['categorie'],
      begin: json['begin'],
      end: json['end'],
      title: json['title'],
      description: json['description'],
      ort: json['ort'],
      fileurl: json['fileurl'],
      canceled: json['canceled'],
    );
  }
}

这是我处理 json 的代码:

  Future<Events> fetchEvents() async {
    final response = await http.get('https://my.url.de/');

    if (response.statusCode == 200) {
      return Events.fromJson(jsonDecode(response.body)); // <-- ERROR ROW
    } else {
      throw Exception('Failed to load events from web');
    }
  }

和我的 json:

[
    {
        "id": "363",
        "categorie_id": "2",
        "categorie": "Cat 1",
        "begin": "2020-12-20 18:30:00",
        "end": null,
        "title": "title 1",
        "description": "",
        "ort": "",
        "fileurl": "",
        "canceled": "1"
    },
    {
        "id": "364",
        "categorie_id": "5",
        "categorie": "Cat 3",
        "begin": "2020-12-28 09:00:00",
        "end": null,
        "title": "title 2",
        "description": "",
        "ort": "",
        "fileurl": "",
        "canceled": "0"
    }
]

实际上我得到了一个错误:

E/flutter ( 4461): [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: type 'List<dynamic>' is not a subtype of type 'Map<dynamic, dynamic>'
E/flutter ( 4461): #0      _EventdetailsState.fetchEvents (package:apptest/main.dart:40:11)
E/flutter ( 4461): <asynchronous suspension>

我正在寻找帮助和解释或初学者的好文档

谢谢!

3 个答案:

答案 0 :(得分:0)

试试below code:-

Future<Events> fetchEvents() async {
    final response = await http.get('https://my.url.de/');

    if (response.statusCode == 200) {
      return Events.fromJson(response.body); 
    } else {
      throw Exception('Failed to load events from web');
    }
  }

答案 1 :(得分:0)

从您的后端收到的 json 是一个地图数组 activation='tanh'。 要正确映射此,您可以尝试

List<Map<String, dynamic>>

答案 2 :(得分:0)

响应是事件列表,您的类是单个事件,即使名称是复数。

你的 jsonDecode(response.body) 将返回一个 List>;

Future<List<Events>> fetchEvents() async { // !!Changed return to List<Events>!!
  final response = await http.get('https://my.url.de/');

  if (response.statusCode == 200) {
    final List<Events> events = [];
    for (var event in jsonDecode(response.body)) {
     events.add(Events.fromJson(event as Map<String, dynamic>));
    }
    return events;
  } else {
    throw Exception('Failed to load events from web');
  }
}