我已经使用Node/MongoDB作为我的后端,一切都工作得很好,wrt请求和响应。
对于我的前端,我正在使用flutter构建一个移动应用程序,因此必须创建模型类来表示我的响应。回复示例:
{success: true, message: Logged in Successfully, user: {_id: 6028965c16056b37eca50076, username: spideyr, email: peterparker@gmail.com, password: $2b$10$R4kYBA3Ezk7z2EBIY3dfk.6Qy.IXQuXJocKVS5PCzLf4fXYckUMju, phone: 89066060484, __v: 0}, accessToken: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE2MTM1NDg3ODIsImV4cCI6MTYxNjE0MDc4MiwiYXVkIjoiNjAyODk2NWMxNjA1NmIzN2VjYTUwMDc2IiwiaXNzIjoicGlja3VycGFnZS5jb20ifQ.DX8-WGRkCQ9geAaQASOIzoPGpvpjdI7aV0C5o1i5Thw, refreshToken: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE2MTM1NDg3ODIsImV4cCI6MTY0NTEwNjM4MiwiYXVkIjoiNjAyODk2NWMxNjA1NmIzN2VjYTUwMDc2IiwiaXNzIjoicGlja3VycGFnZS5jb20ifQ.RkVCGK9FfU0rxs2qf5QtJsyFaGShsL05CI320GsmAwg}在此响应正文中,我只对user字段感兴趣,因此创建了如下POJO/PODO类:
class UserModel {
final String id;
final String username;
final String email;
final String phone;
const UserModel({
this.id,
@required this.email,
@required this.username,
@required this.phone,
});
UserModel copyWith({String id, String username, String email, String phone}){
if (
(id == null) || identical(id, this.id) &&
(username == null) || identical(id, this.username) &&
(email == null || identical(email, this.email)) &&
(phone == null || identical(phone, this.phone))) {
return this;
}
return new UserModel(
id: id ?? this.id,
username: username ?? this.username,
email: email ?? this.email,
phone: phone ?? this.phone,
);
}
static const empty = UserModel(email: '', username: null, phone: null, id: '');
@override
String toString() {
return 'User{id: $id, username: $username, email: $email, phone: $phone}';
}
factory UserModel.fromMap(Map<String, dynamic> map){
return new UserModel(
id:map['_id'], // unable to understand why it shows error here
username:map['username'],
email:map['email'],
phone:map['phone'],
);
}
Map<String, dynamic> toMap(){
return {
'id': id,
'username': username,
'email': email,
'phone': phone,
};
}
}我可以登录并注册用户,但是在我的模型类UserModel.fromJSON()方法中,这个错误一直出现在我从mongo db的_id to id映射的时候。下面是错误:
I/flutter (19353): NoSuchMethodError: The method '[]' was called on null.
I/flutter (19353): Receiver: null
I/flutter (19353): Tried calling: []("_id")有人知道我需要在UserModel类中做哪些更改吗?谢谢。
发布于 2021-02-17 16:40:48
我把你的JSON转换成了类
class Model {
Model({
this.success,
this.message,
this.user,
this.accessToken,
this.refreshToken,
});
bool success;
String message;
User user;
String accessToken;
String refreshToken;
factory Model.fromJson(Map<String, dynamic> json) => Model(
success: json["success"],
message: json["message"],
user: User.fromJson(json["user"]),
accessToken: json["accessToken"],
refreshToken: json["refreshToken"],
);
Map<String, dynamic> toJson() => {
"success": success,
"message": message,
"user": user.toJson(),
"accessToken": accessToken,
"refreshToken": refreshToken,
};
}
class User {
User({
this.id,
this.username,
this.email,
this.password,
this.phone,
this.v,
});
String id;
String username;
String email;
String password;
String phone;
int v;
factory User.fromJson(Map<String, dynamic> json) => User(
id: json["_id"],
username: json["username"],
email: json["email"],
password: json["password"],
phone: json["phone"],
v: json["__v"],
);
Map<String, dynamic> toJson() => {
"_id": id,
"username": username,
"email": email,
"password": password,
"phone": phone,
"__v": v,
};
}发布于 2021-02-17 16:32:43
问题出在UserResponseModel类中。您正在将Map分配给user。
试试这个:
factory UserResponseModel.fromJSON(Map<String, dynamic> json) {
return UserResponseModel(
user: UserModel.fromJSON(json['user']),
success: json['success'],
message: json['message'],
accessToken: json['accessToken'],
refreshToken: json['refreshToken'],
);
}发布于 2021-02-17 21:28:10
感谢所有试图帮助我理解这个问题的人。我的APIClient类中缺少一条返回语句,这确保了fromJSON方法中的映射中的所有值都为空。
更正如下:
class APIClient {
dynamic post(String pathSegment, Map<String, dynamic> body) async {
Dio dio = Dio();
Response response = await dio.post(
'${APIConstants.BASE_URL}${APIConstants.AUTH_URL}/$pathSegment',
data: body
);
if(response.statusCode == 200) {
print(response.data);
return response.data; // here's the change
} else {
print(response.statusMessage);
throw Exception(response.statusMessage);
}
}
}https://stackoverflow.com/questions/66238251
复制相似问题