尝试转换时,Profile 类未正确转换。作为 toString() 函数的结果退出。
个人飞镖
import 'package:adminapp/domains/Test/Profile.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
part 'Person.freezed.dart';
part 'Person.g.dart';
@freezed
class Person with _$Person {
factory Person({
String? id,
Profile? profile,
}) = _Person;
factory Person.fromJson(Map<String, dynamic> json) => _$PersonFromJson(json);
}
个人资料.dart
import 'package:freezed_annotation/freezed_annotation.dart';
part 'Profile.freezed.dart';
part 'Profile.g.dart';
@freezed
class Profile with _$Profile {
factory Profile({
DateTime? bDay,
String? hob,
String? rel,
}) = _Profile;
factory Profile.fromJson(Map<String, dynamic> json) =>
_$ProfileFromJson(json);
}
主要.dart
import 'package:adminapp/domains/Test/Person.dart';
import 'package:adminapp/domains/Test/Profile.dart';
void main(List<String> args) {
Person p = Person(
id: '4',
profile: Profile(
bDay: DateTime.now(),
hob: "123",
rel: 'asd',
));
print(p.toJson());
}
输出:
{id: 4, profile: Profile(bDay: 2021-07-28 08:42:51.708857, hob: 123, rel: asd)}
但它不是json格式!配置文件类转换不正确!而且我不能把它保存到firestore!