微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

解析来自 rest api 的数据时出错 - “尝试调用:cast<Map<String, dynamic>>() Found: cast<RK, RV>() => Map<RK, RV>” in flutter

如何解决解析来自 rest api 的数据时出错 - “尝试调用:cast<Map<String, dynamic>>() Found: cast<RK, RV>() => Map<RK, RV>” in flutter

我正在尝试解析来自网络的数据。这是我的模型类-

import 'dart:convert';

class ModelEvent {
ModelEvent({
required this.id,required this.appName,required this.desc,required this.colorValue,required this.logoFileName,required this.logoFilePath,required this.splashScreenFileName,required this.splashScreenFilePath,required this.userId,required this.createdAt,required this.updatedAt,required this.apptemplate,});

final int id;
final String appName;
final String desc;
final String colorValue;
final String logoFileName;
final String logoFilePath;
final String splashScreenFileName;
final String splashScreenFilePath;
final String userId;
final DateTime createdAt;
final DateTime updatedAt;
final Apptemplate? apptemplate;

factory ModelEvent.fromJson(String str) => ModelEvent.fromMap(json.decode(str));

String toJson() => json.encode(toMap());


factory ModelEvent.fromMap(Map<String,dynamic> json) => ModelEvent(
id: json["id"],appName: json["app_name"],desc: json["desc"],colorValue: json["color_value"],logoFileName: json["logo_file_name"],logoFilePath: json["logo_file_path"],splashScreenFileName: json["splash_screen_file_name"],splashScreenFilePath: json["splash_screen_file_path"],userId: json["user_id"],createdAt: DateTime.parse(json["created_at"]),updatedAt: DateTime.parse(json["updated_at"]),apptemplate: json["apptemplate"] == null ? null : 
Apptemplate.fromMap(json["apptemplate"]),);

Map<String,dynamic> toMap() => {
"id": id,"app_name": appName,"desc": desc,"color_value": colorValue,"logo_file_name": logoFileName,"logo_file_path": logoFilePath,"splash_screen_file_name": splashScreenFileName,"splash_screen_file_path": splashScreenFilePath,"user_id": userId,"created_at": createdAt.toIso8601String(),"updated_at": updatedAt.toIso8601String(),"apptemplate": apptemplate == null ? null : apptemplate!.toMap(),};
}

class Apptemplate {
Apptemplate({
required this.template,required this.appinfoId,});

final String template;
final String appinfoId;

factory Apptemplate.fromJson(String str) => Apptemplate.fromMap(json.decode(str));

String toJson() => json.encode(toMap());

factory Apptemplate.fromMap(Map<String,dynamic> json) => Apptemplate(
template: json["template"],appinfoId: json["appinfo_id"],dynamic> toMap() => {
"template": template,"appinfo_id": appinfoId,};
}

方法是 -

List<ModelEvent> parseEvents(String responseBody) { //calling this method shows the error
final parsed = jsonDecode(responseBody).cast<Map<String,dynamic>>();

return parsed.map<ModelEvent>((json) => ModelEvent.fromJson(json)).toList();

}

Future<List<ModelEvent>> fetchEvents(http.Client client) async {
final response =
await http.post(Uri.parse(url),headers:{
  'Authorization': 'Bearer $token',}
);
print(response.statusCode);
if (response.statusCode == 200) {
  this.setState(() {
    eventList = parseEvents(response.body);
  });

  return parseEvents(response.body);
} else {
  //If the server did not return a 200 OK response,// then throw an exception.
  throw Exception('Failed to load');
}

在控制台中它说 - “未处理的异常:NoSuchMethodError:类 '_InternalLinkedHashMap' 没有具有匹配参数的实例方法 'cast'。” 还有“尝试调用:cast>() Found: cast() => Map” 这是控制台的图像-

Image

解决方法

试试这个 Map<String,dynamic> parsed = jsonDecode(responseBody);

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。