_从 API 映射数据时出现类型错误

问题描述 投票:0回答:1

我正在尝试使用this API。我正在发送 GET 请求,我得到的数据如下所示;

{
    "info": {
        "count": 826,
        "pages": 42,
        "next": "https://rickandmortyapi.com/api/character?page=2",
        "prev": null
    },
    "results": [
        {
            "id": 1,
            "name": "Rick Sanchez",
            "status": "Alive",
            "species": "Human",
            "type": "",
            "gender": "Male",
            "origin": {
                "name": "Earth (C-137)",
                "url": "https://rickandmortyapi.com/api/location/1"
            },
            "location": {
                "name": "Citadel of Ricks",
                "url": "https://rickandmortyapi.com/api/location/3"
            },
            "image": "https://rickandmortyapi.com/api/character/avatar/1.jpeg",
            "episode": [
                "https://rickandmortyapi.com/api/episode/1",
                ...
            ],
            "url": "https://rickandmortyapi.com/api/character/1",
            "created": "2017-11-04T18:48:46.250Z"
        },
        //and the next character...

这是一个页面。所以我在页面中得到一个名为“info”的对象和一个名为“results”的对象列表。

我有一个用于列表结果对象的类。看起来像这样;

class Character {
  final int id;
  final String name;
  final String status;
  final String species;
  final String type;
  final String gender;
  final CharacterLocation origin;
  final CharacterLocation location;
  final String image;
  final List<String> episode;
  final String url;
  final DateTime created;

  Character({
    required this.id,
    required this.name,
    required this.status,
    required this.species,
    required this.type,
    required this.gender,
    required this.origin,
    required this.location,
    required this.image,
    required this.episode,
    required this.url,
    required this.created,
  });

  factory Character.fromJson(Map<String, dynamic> json) => Character(
        id: json["id"],
        name: json["name"],
        status: json["status"],
        species: json["species"],
        type: json["type"],
        gender: json["gender"],
        origin: CharacterLocation.fromJson(json["origin"]),
        location: CharacterLocation.fromJson(json["location"]),
        image: json["image"],
        episode: List<String>.from(json["episode"].map((x) => x)),
        url: json["url"],
        created: DateTime.parse(json["created"]),
      );
}

class CharacterLocation {
  CharacterLocation({
    required this.name,
    required this.url,
  });

  String name;
  String url;

  factory CharacterLocation.fromJson(Map<String, dynamic> json) =>
      CharacterLocation(
        name: json["name"],
        url: json["url"],
      );

  Map<String, dynamic> toJson() => {
        "name": name,
        "url": url,
      };
}

所以结果应该是一个名为Character的类的类型的对象列表。

这是我的页面模型

import 'package:flutter_learn_practice/ram_character_models.dart';

class CharacterPage {
  Info info;
  List<Character> results;

  CharacterPage({
    required this.info,
    required this.results});
  
  factory CharacterPage.fromJson(Map<String, dynamic> json) => CharacterPage(
    info: Info.fromJson(json["info"]),
    results: List<Character>.from(json["results"].map((x) => x))
    );
}

class Info {
  int count;
  int pages;
  String? next;
  String? prev;

  Info({
    required this.count,
    required this.pages,
    this.next = '',
    this.prev = '',
  });

  factory Info.fromJson(Map<String, dynamic> json) => Info(
    count: json["count"],
    pages: json["pages"],
    next: json["next"],
    prev: json["prev"],
  );
}

我可以成功使用“信息”及其实例(我不知道这是否是正确的词),例如页数或计数。但每当我尝试使用“结果”的实例时,我都会收到以下错误;

“发生异常。

_TypeError(类型“_Map”不是“字符”类型的子类型)”

我被困在这里好几天了,终于决定寻求帮助。

我希望我能够清楚地解释我的问题。预先感谢。

html flutter dart dart-html
1个回答
0
投票

results
是代表各个字符的对象列表。您正在尝试直接访问此列表的元素,就好像它是字符对象一样。

要解决此问题,您需要迭代结果列表,其类型为

List<dynamic>
。 对于列表中的每个元素,您需要使用
Character.fromJson
工厂构造函数将其转换为 Character 对象。

class CharacterPage {
  Info info;
  List<Character> results;

  CharacterPage({
    required this.info,
    required this.results,
  });

  factory CharacterPage.fromJson(Map<String, dynamic> json) => CharacterPage(
        info: Info.fromJson(json["info"]),
        results: List<Character>.from(json["results"].map((x) => Character.fromJson(x))), // Use map to convert each element
      );
}
© www.soinside.com 2019 - 2024. All rights reserved.