Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 自定义反序列化dart对象_Flutter_Dart - Fatal编程技术网

Flutter 自定义反序列化dart对象

Flutter 自定义反序列化dart对象,flutter,dart,Flutter,Dart,我的课程有: class Student { String name; String family; } 但是服务器会给我一个json,比如: { "name": "mohammad", "family": "nasr", "age": 23, "phone": "+9345687544", } 我想要的是一个类似于: @Json

我的课程有:

class Student {
  String name;
  String family;
}
但是服务器会给我一个json,比如:

{
  "name": "mohammad",
  "family": "nasr",
  "age": 23,
  "phone": "+9345687544",
}
我想要的是一个类似于:

@JsonSerializable()
class JsonResponse {
  Student student;
  Info info;
}
但问题是,我将给出jsondeconding错误,因为我的
JsonResponse
类与服务器响应不匹配


但我希望我的
JsonResponse
位于该表单上,

只需从JSON创建一个反序列化方法,如下所示:

class Info {
  int age;
  String phone;

  Info({this.age, this.phone});

  factory Info.fromJson(Map<String, dynamic> json) {
    if(json == null) return null;
    
    return Info(
      age: json['age'],
      phone: json['phone'],
    );
  }
}

class Student {
  String name;
  String family;

  Student({this.name, this.family});

  factory Student.fromJson(Map<String, dynamic> json) {
    if(json == null) return null;
    
    return Student(
      name: json['name'],
      family: json['family'],
    );
  }
}

class JsonResponse {
  Student student;
  Info info;

  JsonResponse({this.student, this.info});

  factory JsonResponse.fromJson(Map<String, dynamic> json) {
    if(json == null) return null;
    
    Student student = Student.fromJson(json);
    Info info = Info.fromJson(json);

    return JsonResponse(student: student, info: info);
  }
}
类信息{
智力年龄;
字符串电话;
信息({this.age,this.phone});
factory Info.fromJson(映射json){
if(json==null)返回null;
返回信息(
年龄:json['age'],
电话:json['phone'],
);
}
}
班级学生{
字符串名;
弦族;
学生({this.name,this.family});
factory Student.fromJson(映射json){
if(json==null)返回null;
留学生(
名称:json['name'],
family:json['family'],
);
}
}
类JsonResponse{
学生;
信息;
JsonResponse({this.student,this.info});
工厂JsonResponse.fromJson(映射json){
if(json==null)返回null;
Student=Student.fromJson(json);
Info=Info.fromJson(json);
返回JsonResponse(学生:学生,信息:信息);
}
}

来自json的
fromJson
将从服务器接收
json
响应,并将其转换为相应的模型对象。

如果希望JsonResponse类采用

@JsonSerializable(explicitToJson: true)
class JsonResponse {
  Student student;
  Info info;
}
并序列化类,您的响应将以
{student:{name:mohammad,family:nasr},info:{age:23,phone:+9345687544},
的形式出现,因此您应该更改JsonResponse类或使用此

JsonResponse jsonResponse = JsonResponse.fromJson(
  Student.fromJson(jsonData), Info.fromJson(jsonData),
);

是的,但是我想用
JsonSerializable
包来实现它,它给了我们代码生成的能力。
JsonResponse jsonResponse = JsonResponse.fromJson(
  Student.fromJson(jsonData), Info.fromJson(jsonData),
);