[英]Unhandled Exception: type 'String' is not a subtype of type 'Map<String, dynamic>'
I'm not exactly sure whats happened because my code was working fine but all of a sudden stopped working and I dont think I changed anything in relation to this.我不确定发生了什么,因为我的代码工作正常,但突然停止工作,我认为我没有改变任何与此相关的内容。 I've been stuck on this for two days and can't figure out what I'm doing wrong.
我已经坚持了两天,无法弄清楚我做错了什么。 The data being returned from the API is correct and it returns with a status code of 200
从 API 返回的数据是正确的,返回状态码为 200
I believe the error is something to do with the PTWorkoutPlanItem.fromJson()
function.我相信该错误与
PTWorkoutPlanItem.fromJson()
function 有关。 I dont think it has anything to do with the List<AssignedUsers>
or List<WorkoutPlanItem>
models in the PTWorkoutPlanItem
model as when I change these to dynamic
, the error still occurs我认为这与 PTWorkoutPlanItem model 中的
List<AssignedUsers>
或 List< PTWorkoutPlanItem
List<WorkoutPlanItem>
模型有任何关系,因为当我将这些更改为dynamic
时,错误仍然发生
The error says错误说
[ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: type 'String' is not a subtype of type 'Map<String, dynamic>'
code to get the data:获取数据的代码:
getPlanData() async {
var data = PTWorkoutPlanItem.fromJson(
await get(url: 'pt/workouts/plan/' + planId.toString()));
}
// Console logged the response here and it is correct, so this function works
Future<dynamic> get({url}) async {
final response = await http.get(baseUrl + url, headers: headers);
print(response.statusCode);
dynamic res = json.decode(response.body);
return res;
}
models楷模
class PTWorkoutPlanItem {
int id;
String title;
List<AssignedUsers> assignedUsers;
List<WorkoutPlanItem> workouts;
int ptId;
PTWorkoutPlanItem(
{this.id, this.title, this.assignedUsers, this.workouts, this.ptId});
factory PTWorkoutPlanItem.fromJson(Map<String, dynamic> json) {
return PTWorkoutPlanItem(
id: json['id'],
title: json['title'],
assignedUsers: List.from(json['assignedUsers'])
.map((item) => AssignedUsers.fromJson(item))
.toList(),
workouts: List.from(json['workouts'])
.map((item) => WorkoutPlanItem.fromJson(item))
.toList(),
ptId: json['ptId'],
);
}
}
class AssignedUsers {
int id;
String title;
AssignedUsers({this.id, this.title});
factory AssignedUsers.fromJson(Map<String, dynamic> json) {
return AssignedUsers(id: json['id'], title: json['title']);
}
}
class WorkoutPlanItem {
int id;
String title;
int duration;
int sets;
int rest;
WorkoutPlanItem({this.id, this.title, this.duration, this.sets, this.rest});
factory WorkoutPlanItem.fromJson(Map<String, dynamic> json) {
return WorkoutPlanItem(
id: json['id'],
title: json['title'],
duration: json['duration'],
sets: json['sets'],
rest: json['rest'],
);
}
}
this is what gets retuned in the API这是在 API 中重新调整的内容
{
"id": 1,
"title": "Pull day",
"assignedUsers": [
{
"id": 1,
"title": "josh"
},
{
"id": 2,
"title": "marus"
}
],
"workouts": [
{
"id": 4,
"title": "Workout item 4",
"duration": 10,
"sets": 3,
"rest": 3
},
{
"id": 1,
"title": "Workout item 1",
"duration": 10,
"sets": 3,
"rest": 3
}
],
"ptId": 1
}
Try this.尝试这个。 I have added a lot of type casts but the most important part is how I handle the
List<dynamic>
and cast each element to Map<String, dynamic>
:我添加了很多类型转换,但最重要的部分是我如何处理
List<dynamic>
并将每个元素转换为Map<String, dynamic>
:
class PTWorkoutPlanItem {
int id;
String title;
List<AssignedUsers> assignedUsers;
List<WorkoutPlanItem> workouts;
int ptId;
PTWorkoutPlanItem(
{this.id, this.title, this.assignedUsers, this.workouts, this.ptId});
factory PTWorkoutPlanItem.fromJson(Map<String, dynamic> json) {
return PTWorkoutPlanItem(
id: json['id'] as int,
title: json['title'] as String,
assignedUsers: (json['assignedUsers'] as List<dynamic>)
.cast<Map<String, dynamic>>()
.map((item) => AssignedUsers.fromJson(item))
.toList(),
workouts: (json['workouts'] as List<dynamic>)
.cast<Map<String, dynamic>>()
.map((item) => WorkoutPlanItem.fromJson(item))
.toList(),
ptId: json['ptId'] as int,
);
}
}
class AssignedUsers {
int id;
String title;
AssignedUsers({this.id, this.title});
factory AssignedUsers.fromJson(Map<String, dynamic> json) {
return AssignedUsers(id: json['id'] as int, title: json['title'] as String);
}
}
class WorkoutPlanItem {
int id;
String title;
int duration;
int sets;
int rest;
WorkoutPlanItem({this.id, this.title, this.duration, this.sets, this.rest});
factory WorkoutPlanItem.fromJson(Map<String, dynamic> json) {
return WorkoutPlanItem(
id: json['id'] as int,
title: json['title'] as String,
duration: json['duration'] as int,
sets: json['sets'] as int,
rest: json['rest'] as int,
);
}
}
Also, here is a Dart 2.12 compatible version where required
as been added to the named parameters since they are all not allowed to be null
and does not have any default value:此外,这是一个 Dart 2.12 兼容版本,
required
添加到命名参数中,因为它们都不允许是null
并且没有任何默认值:
class PTWorkoutPlanItem {
int id;
String title;
List<AssignedUsers> assignedUsers;
List<WorkoutPlanItem> workouts;
int ptId;
PTWorkoutPlanItem(
{required this.id,
required this.title,
required this.assignedUsers,
required this.workouts,
required this.ptId});
factory PTWorkoutPlanItem.fromJson(Map<String, dynamic> json) {
return PTWorkoutPlanItem(
id: json['id'] as int,
title: json['title'] as String,
assignedUsers: (json['assignedUsers'] as List<dynamic>)
.cast<Map<String, dynamic>>()
.map((item) => AssignedUsers.fromJson(item))
.toList(),
workouts: (json['workouts'] as List<dynamic>)
.cast<Map<String, dynamic>>()
.map((item) => WorkoutPlanItem.fromJson(item))
.toList(),
ptId: json['ptId'] as int,
);
}
}
class AssignedUsers {
int id;
String title;
AssignedUsers({required this.id, required this.title});
factory AssignedUsers.fromJson(Map<String, dynamic> json) {
return AssignedUsers(id: json['id'] as int, title: json['title'] as String);
}
}
class WorkoutPlanItem {
int id;
String title;
int duration;
int sets;
int rest;
WorkoutPlanItem(
{required this.id,
required this.title,
required this.duration,
required this.sets,
required this.rest});
factory WorkoutPlanItem.fromJson(Map<String, dynamic> json) {
return WorkoutPlanItem(
id: json['id'] as int,
title: json['title'] as String,
duration: json['duration'] as int,
sets: json['sets'] as int,
rest: json['rest'] as int,
);
}
}
Update: Can you also try change your method into this:更新:您也可以尝试将您的方法更改为:
Future<Map<String, dynamic>> get({url}) async {
final response = await http.get(baseUrl + url, headers: headers);
print(response.statusCode);
final res = json.decode(response.body) as Map<String, dynamic>;
return res;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.