Json可序列化为 Json 抛出 return 错误
JsonSerializable to Json throws return error
我正在尝试使用这个包 - https://pub.dev/packages/json_serializable 来帮助我解析来自 api 的数据。但是我遇到了下面的错误,我不知道如何解决它。
我收到这个错误 A value of type 'Location' can't be returned from the method 'toJson' because it has a return type of 'Map<String, dynamic>
@JsonSerializable()
class Location {
const Location({
required this.title,
required this.locationType,
required this.latLng,
required this.woeid,
});
final String title;
final LocationType locationType;
final LatLng latLng;
final int woeid;
factory Location.fromJson(Map<String,dynamic> json) => _$LocationFromJson(json);
Map<String, dynamic> toJson() => _$LocationFromJson(this);
}
每当我将最后一部分更改为 Location toJson() => _$LocationFromJson(this);
时,我都会收到错误 The argument type 'Location' can't be assigned to the parameter type 'Map<String, dynamic>
您需要将复杂的数据类型转换为简单的数据类型,如String、int、double等,以便在json中进行解析。下面是一个关于如何实现纬度和经度的示例。
假设我有一个学校数据,我想为其存储名称和位置。我会为名称创建一个简单的字符串,但为位置创建一个单独的 class。以下是它的运行方式:
class SchoolDataModel {
final String name;
final SchoolLocation schoolLocation;
SchoolDataModel({
required this.name,
required this.schoolLocation,
});
factory SchoolDataModel.fromJson(Map<String, dynamic> parsedJson) =>
SchoolDataModel(
name: parsedJson['name'] as String,
schoolLocation: SchoolLocation.fromJson(
parsedJson['location'] as Map<String, dynamic>),
);
}
如您所见,SchoolLocation.fromJson用于使用自己的构造函数解析位置数据。如下所示:
class SchoolLocation {
final String latitude;
final String longitude;
SchoolLocation({
required this.latitude,
required this.longitude,
});
factory SchoolLocation.fromJson(Map<String, dynamic> parsedJson) =>
SchoolLocation(
latitude: parsedJson['latitude'] as String,
longitude: parsedJson['longitude'] as String,
);
}
我正在尝试使用这个包 - https://pub.dev/packages/json_serializable 来帮助我解析来自 api 的数据。但是我遇到了下面的错误,我不知道如何解决它。
我收到这个错误 A value of type 'Location' can't be returned from the method 'toJson' because it has a return type of 'Map<String, dynamic>
@JsonSerializable()
class Location {
const Location({
required this.title,
required this.locationType,
required this.latLng,
required this.woeid,
});
final String title;
final LocationType locationType;
final LatLng latLng;
final int woeid;
factory Location.fromJson(Map<String,dynamic> json) => _$LocationFromJson(json);
Map<String, dynamic> toJson() => _$LocationFromJson(this);
}
每当我将最后一部分更改为 Location toJson() => _$LocationFromJson(this);
时,我都会收到错误 The argument type 'Location' can't be assigned to the parameter type 'Map<String, dynamic>
您需要将复杂的数据类型转换为简单的数据类型,如String、int、double等,以便在json中进行解析。下面是一个关于如何实现纬度和经度的示例。
假设我有一个学校数据,我想为其存储名称和位置。我会为名称创建一个简单的字符串,但为位置创建一个单独的 class。以下是它的运行方式:
class SchoolDataModel {
final String name;
final SchoolLocation schoolLocation;
SchoolDataModel({
required this.name,
required this.schoolLocation,
});
factory SchoolDataModel.fromJson(Map<String, dynamic> parsedJson) =>
SchoolDataModel(
name: parsedJson['name'] as String,
schoolLocation: SchoolLocation.fromJson(
parsedJson['location'] as Map<String, dynamic>),
);
}
如您所见,SchoolLocation.fromJson用于使用自己的构造函数解析位置数据。如下所示:
class SchoolLocation {
final String latitude;
final String longitude;
SchoolLocation({
required this.latitude,
required this.longitude,
});
factory SchoolLocation.fromJson(Map<String, dynamic> parsedJson) =>
SchoolLocation(
latitude: parsedJson['latitude'] as String,
longitude: parsedJson['longitude'] as String,
);
}