将对象列表从Json转换为类结构时出现类型错误

将对象列表从Json转换为类结构时出现类型错误,json,dynamic,flutter,dart,typeerror,Json,Dynamic,Flutter,Dart,Typeerror,我试图将对象列表从JSON转换为类结构。我遇到了一些我似乎无法回避的类型错误 我已经设置了一些方法来将每个对象转换成JSON和从JSON转换成JSON,它们各自工作得很好,我并没有遇到任何问题。问题是当我试着把所有的部分都列在一起做的时候 我的代码在哪里得到错误: var temp = json.decode(response.body); var shifts = ShiftList.fromJson(temp); // This is where it breaks cla

我试图将对象列表从JSON转换为类结构。我遇到了一些我似乎无法回避的类型错误

我已经设置了一些方法来将每个对象转换成JSON和从JSON转换成JSON,它们各自工作得很好,我并没有遇到任何问题。问题是当我试着把所有的部分都列在一起做的时候

我的代码在哪里得到错误:

    var temp = json.decode(response.body);
    var shifts = ShiftList.fromJson(temp); // This is where it breaks
class UserList {
  List<UserObject> users;
  UserList({
    this.users,
  });

  addUser(UserObject user){
    if(users == null){
      users = [user];
    }else{
      users.add(user);
    }
  }

  factory UserList.fromJson(Map<String, dynamic> json) {
    return UserList(
      users: _toObjectList(json['UserVal'], (e) => UserObject.fromJson(e)),
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'UserVal': _fromList(users, (e) => e.toJson()),
    };
  }

}

List _fromList(data, Function(dynamic) toJson) {
  if (data == null) {
    return null;
  }
  var result = [];
  for (var element in data) {
    var value;
    if (element != null) {
      value = toJson(element);
    }
    result.add(value);
  }
  return result;
}

List<T> _toObjectList<T>(data, T Function(Map<String, dynamic>) fromJson) {
  if (data == null) {
    return null;
  }
  var result = <T>[];
  for (var element in data) {
    T value;
    if (element != null) {
      value = fromJson(element as Map<String, dynamic>);
    }
    result.add(value);
  }
  return result;
}
移位列表类别:

   class ShiftList {
  List<Shift> shifts;
  ShiftList({
    this.shifts,
  });

  addShift(Shift shift){
    if(shifts == null){
      shifts = [shift];
    }else{
      shifts.add(shift);
    }
  }

  factory ShiftList.fromJson(Map<String, dynamic> json) {
    return ShiftList(
      shifts: _toObjectList(json['Shifts'], (e) => Shift.fromJson(e)),
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'Shifts': _fromList(shifts, (e) => e.toJson()),
    };
  }

}

List _fromList(data, Function(dynamic) toJson) {
  if (data == null) {
    return null;
  }
  var result = [];
  for (var element in data) {
    var value;
    if (element != null) {
      value = toJson(element);
    }
    result.add(value);
  }
  return result;
}

List<T> _toObjectList<T>(data, T Function(Map<String, dynamic>) fromJson) {
  if (data == null) {
    return null;
  }
  var result = <T>[];
  for (var element in data) {
    T value;
    if (element != null) {
      value = fromJson(element as Map<String, dynamic>);
    }
    result.add(value);
  }
  return result;
}

不要创建包含列表的新对象,只需让shift类包含list属性即可

class Shift {
  String Name;
  String AccountID;
  String Identifier;
  List<UserObject> UserVal;
  ReportList Reports; // do same for this
  TaskList Tasks; // do same for this
  String Scheduling;
  String ShiftManagerCode;

  Shift({
    this.AccountID,
    this.Name,
    this.ShiftManagerCode,
    this.Identifier,
    this.UserVal,
    this.Reports,
    this.Tasks,
    this.Scheduling,
  });

  factory Shift.fromJson(Map<String, dynamic> json) {
    return Shift(
      AccountID: json['AccountID'] as String,
      Identifier: json['Identifier'] as String,
      Name: json['ShiftName'] as String,
      ShiftManagerCode: json['ShiftManagerCode'] as String,
      UserVal: json['UserVal'] == null ? List<UserObject> : (json['UserVal'] as List<Dynamic>).map((dynamic map) => UserObject.fromJson(map)).toList(),
      Reports: json['Reports'] as ReportList,
      Tasks: json['Tasks'] as TaskList,
      Scheduling: json['Scheduling'] as String,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'ShiftName': this.Name,
      'AccountID': this.AccountID,
      'Identifier': this.Identifier,
      'ShiftManagerCode': this.ShiftManagerCode,
      'UserVal': this.UserVal,
      'Reports': this.Reports,
      'Tasks': this.Tasks,
      'Scheduling': this.Scheduling,
    };
  }
}
班次{
字符串名;
字符串AccountID;
字符串标识符;
列出UserVal;
ReportList报告;//对此执行相同操作
TaskList Tasks;//对此执行相同的操作
字符串调度;
字符串移位管理代码;
移位({
这是AccountID,
这个,名字,,
此.shiftManager代码,
这个,标识符,,
这是UserVal,
这个,报道,,
这,任务,,
这,日程安排,,
});
factory Shift.fromJson(映射json){
回程班次(
AccountID:json['AccountID']作为字符串,
标识符:json['Identifier']作为字符串,
名称:json['ShiftName']作为字符串,
ShiftManagerCode:json['ShiftManagerCode']作为字符串,
UserVal:json['UserVal']==null?列表:(json['UserVal']作为列表).map((动态映射)=>UserObject.fromJson(映射)).toList(),
报告:json['Reports']作为报告列表,
任务:json['Tasks']作为任务列表,
调度:json['Scheduling']作为字符串,
);
}
映射到JSON(){
返回{
“ShiftName”:此.Name,
“AccountID”:this.AccountID,
“标识符”:此.Identifier,
“ShiftManagerCode”:this.ShiftManagerCode,
“UserVal”:this.UserVal,
“报告”:此。报告,
“任务”:此.Tasks,
“调度”:此。调度,
};
}
}

现在,您可以直接在Json中进行转换。您必须在toJson中做额外的工作,使其正确映射。

不要创建包含列表的新对象,只需让shift类包含list属性即可

class Shift {
  String Name;
  String AccountID;
  String Identifier;
  List<UserObject> UserVal;
  ReportList Reports; // do same for this
  TaskList Tasks; // do same for this
  String Scheduling;
  String ShiftManagerCode;

  Shift({
    this.AccountID,
    this.Name,
    this.ShiftManagerCode,
    this.Identifier,
    this.UserVal,
    this.Reports,
    this.Tasks,
    this.Scheduling,
  });

  factory Shift.fromJson(Map<String, dynamic> json) {
    return Shift(
      AccountID: json['AccountID'] as String,
      Identifier: json['Identifier'] as String,
      Name: json['ShiftName'] as String,
      ShiftManagerCode: json['ShiftManagerCode'] as String,
      UserVal: json['UserVal'] == null ? List<UserObject> : (json['UserVal'] as List<Dynamic>).map((dynamic map) => UserObject.fromJson(map)).toList(),
      Reports: json['Reports'] as ReportList,
      Tasks: json['Tasks'] as TaskList,
      Scheduling: json['Scheduling'] as String,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'ShiftName': this.Name,
      'AccountID': this.AccountID,
      'Identifier': this.Identifier,
      'ShiftManagerCode': this.ShiftManagerCode,
      'UserVal': this.UserVal,
      'Reports': this.Reports,
      'Tasks': this.Tasks,
      'Scheduling': this.Scheduling,
    };
  }
}
班次{
字符串名;
字符串AccountID;
字符串标识符;
列出UserVal;
ReportList报告;//对此执行相同操作
TaskList Tasks;//对此执行相同的操作
字符串调度;
字符串移位管理代码;
移位({
这是AccountID,
这个,名字,,
此.shiftManager代码,
这个,标识符,,
这是UserVal,
这个,报道,,
这,任务,,
这,日程安排,,
});
factory Shift.fromJson(映射json){
回程班次(
AccountID:json['AccountID']作为字符串,
标识符:json['Identifier']作为字符串,
名称:json['ShiftName']作为字符串,
ShiftManagerCode:json['ShiftManagerCode']作为字符串,
UserVal:json['UserVal']==null?列表:(json['UserVal']作为列表).map((动态映射)=>UserObject.fromJson(映射)).toList(),
报告:json['Reports']作为报告列表,
任务:json['Tasks']作为任务列表,
调度:json['Scheduling']作为字符串,
);
}
映射到JSON(){
返回{
“ShiftName”:此.Name,
“AccountID”:this.AccountID,
“标识符”:此.Identifier,
“ShiftManagerCode”:this.ShiftManagerCode,
“UserVal”:this.UserVal,
“报告”:此。报告,
“任务”:此.Tasks,
“调度”:此。调度,
};
}
}

现在,您可以直接在Json中进行转换。您必须在toJson中做额外的工作才能使其正确映射。

在不深入实现的情况下,
json.decode(response.body)
将返回一个
列表
类型。您正在期待一个
列表而不是深入实现,
json.decode(response.body)
正在返回一个
列表
类型。您预期的是一个
列表—您不能只是强制转换它—您必须从json解析它。因此,将json['UserVal']替换为UserList。Do
UserVal:UserList.fromJson(json['UserVal'])
我仍然得到一个稍微不同的强制转换错误,但是在修改这3个对象以这种方式强制转换后:
flatter:type'List'不是type'Map'的子类型。
添加了一个关于如何更改fromJson的答案。您不能只强制转换它,您必须从json解析它。因此,将json['UserVal']替换为UserList。Do
UserVal:UserList.fromJson(json['UserVal'])
我仍然得到一个稍微不同的强制转换错误,但是在修改这3个对象以这种方式强制转换之后:
Flatter:type'List'不是type'Map'的子类型。
添加了一个关于您可能希望如何更改fromJson的答案。
{
    "Shifts": [
        {
            "UserVal": [
                {
                    "UID": "test",
                    "Email": "test@gmail.com",
                    "Phone": "0000000000",
                    "Name": "James"
                },
                {
                    "UID": "test",
                    "Email": "test@gmail.com",
                    "Phone": "0000000000",
                    "Name": "Jacob"
                }
            ],
            "AccountID": "1295",
            "Identifier": "JhfSC",
            "ShiftManagerCode": "15A4",
            "Reports": [
                {
                    "Quantity": "3",
                    "User": "Jacob",
                    "Supplies": "ItemA",
                    "Subject": "Supplies",
                    "Note": "test"
                }
            ],
            "Scheduling": "EMPTY",
            "ShiftName": "AF 37",
            "Tasks": [
                {
                    "Status": "done",
                    "User": "James",
                    "Description": "Description here",
                    "Name": "TaskName here"
                }
            ]
        }
    ]
}
class Shift {
  String Name;
  String AccountID;
  String Identifier;
  List<UserObject> UserVal;
  ReportList Reports; // do same for this
  TaskList Tasks; // do same for this
  String Scheduling;
  String ShiftManagerCode;

  Shift({
    this.AccountID,
    this.Name,
    this.ShiftManagerCode,
    this.Identifier,
    this.UserVal,
    this.Reports,
    this.Tasks,
    this.Scheduling,
  });

  factory Shift.fromJson(Map<String, dynamic> json) {
    return Shift(
      AccountID: json['AccountID'] as String,
      Identifier: json['Identifier'] as String,
      Name: json['ShiftName'] as String,
      ShiftManagerCode: json['ShiftManagerCode'] as String,
      UserVal: json['UserVal'] == null ? List<UserObject> : (json['UserVal'] as List<Dynamic>).map((dynamic map) => UserObject.fromJson(map)).toList(),
      Reports: json['Reports'] as ReportList,
      Tasks: json['Tasks'] as TaskList,
      Scheduling: json['Scheduling'] as String,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'ShiftName': this.Name,
      'AccountID': this.AccountID,
      'Identifier': this.Identifier,
      'ShiftManagerCode': this.ShiftManagerCode,
      'UserVal': this.UserVal,
      'Reports': this.Reports,
      'Tasks': this.Tasks,
      'Scheduling': this.Scheduling,
    };
  }
}