生成部分“UserModel.g.dart”后;成功,我在尝试做时遇到以下错误
if (user != null) {
var uid = user.providerData.first.uid;
var displayName = user.providerData.first.displayName;
var email = user.providerData.first.email;
var phoneNumber = user.providerData.first.phoneNumber;
var providerId = user.providerData.first.providerId;
var photoUrl = user.providerData.first.photoURL;
UserModel userModel = UserModel(phoneNumber,
uid: uid,
displayName: displayName,
email: email,
providerId: providerId,
photoUrl: photoUrl);
Query query = users.where('uid', isEqualTo: uid);
query.get().then((querySnapshot) => {
if (querySnapshot.size < 1) {addUser(userModel)}
});
}
}
这是我的 UserModel.dart,文件中没有任何错误。
import 'package:json_annotation/json_annotation.dart';
part 'UserModel.g.dart';
@JsonSerializable()
class UserModel {
late String? uid;
late String? displayName;
late String? email;
late String? phoneNumber;
late String providerId;
late String? photoUrl;
UserModel(this.phoneNumber, {required this.uid, required this.displayName, required this.email,
required this.providerId, required this.photoUrl});
factory UserModel.fromJson(Map<String, dynamic> json) => _$UserModelFromJson(json);
Map<String, dynamic> toJson() => _$UserModelToJson(this);
}
当然,我可以手动映射每个值。但正如这里建议的那样https://flutter.dev/docs/development/data-and-backend/json,以防止
当您的项目变得更大时,手动解码效果不佳。手动编写解码逻辑会变得难以管理且容易出错。如果您在访问不存在的 JSON 字段时出现拼写错误,您的代码会在运行时引发错误。
我使用插件 json_serializable 对其进行自动化处理。但我收到以下错误:预期类型为“Map<String, dynamic>”的值,但得到类型为“UserModel$”的值之一
这个错误是由于不兼容而导致的还是我做错了什么?请回复。非常感谢。:)