Flutter:无法使用属性地理点生成 user.g.dart
Flutter: Could not generate user.g.dart with atribute geopoint
我想序列化用户 class 但是当我执行 flutter pub 运行 build_runner build 时位置会产生错误。我该如何解决?
用户class
...
part 'user.g.dart';
@JsonSerializable()
class UserData {
final String uid;
late final String name;
final String surname;
final List<Location> location;
UserData(
{required this.uid,
required this.name,
required this.surname,
required this.location});
factory UserData.fromJson(Map<String, dynamic> json) => _$UserFromJson(json);
Map<String, dynamic> toJson() => _$UserToJson(this);
}
@JsonSerializable()
class Location {
String locationName;
GeoPoint point;
Location({required this.locationName, required this.point});
factory Location.fromJson(Map<String, dynamic> json) =>
_$LocationFromJson(json);
Map<String, dynamic> toJson() => _$LocationToJson(this);
}
在
location.g.dart
您可能需要在 _$LocationFromJson{}
中手动添加以下代码
point: json['geopoint'] as GeoPoint
我想序列化用户 class 但是当我执行 flutter pub 运行 build_runner build 时位置会产生错误。我该如何解决?
用户class
...
part 'user.g.dart';
@JsonSerializable()
class UserData {
final String uid;
late final String name;
final String surname;
final List<Location> location;
UserData(
{required this.uid,
required this.name,
required this.surname,
required this.location});
factory UserData.fromJson(Map<String, dynamic> json) => _$UserFromJson(json);
Map<String, dynamic> toJson() => _$UserToJson(this);
}
@JsonSerializable()
class Location {
String locationName;
GeoPoint point;
Location({required this.locationName, required this.point});
factory Location.fromJson(Map<String, dynamic> json) =>
_$LocationFromJson(json);
Map<String, dynamic> toJson() => _$LocationToJson(this);
}
在
location.g.dart
您可能需要在 _$LocationFromJson{}
point: json['geopoint'] as GeoPoint