Flutter解析Rick and Morty API数据时触发_TypeError类型错误
问题:解析Rick and Morty API分页数据时results列表类型转换错误
我正在使用Rick and Morty 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" } // 更多角色数据... ] }
该数据包含info分页信息对象和results角色列表。我定义了Character类映射角色对象:
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, }; }
同时定义了CharacterPage类映射整个分页数据:
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"], ); }
目前info相关字段(如count、pages)可正常使用,但访问results列表时抛出错误:
Exception has occurred.
_TypeError (type '_Map<String, dynamic>' is not a subtype of type 'Character')
解决方案
错误根源在于CharacterPage.fromJson方法中,results的映射逻辑没有将JSON对象转换为Character实例。原代码直接把json["results"]中的每个_Map<String, dynamic>对象当作Character类型使用,导致类型不匹配。
修改CharacterPage.fromJson中的results赋值代码,调用Character.fromJson(x)将每个JSON对象转换为Character实例:
factory CharacterPage.fromJson(Map<String, dynamic> json) => CharacterPage( info: Info.fromJson(json["info"]), results: List<Character>.from(json["results"].map((x) => Character.fromJson(x))) );
这样处理后,json["results"]中的每个Map都会被正确转换为Character对象,List<Character>的类型匹配,错误即可解决。
内容的提问来源于stack exchange,提问作者Doğan Erarslan
相关产品推荐
相关产品推荐

