首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何从JSON Response Flutter/MongoDB映射模型类

如何从JSON Response Flutter/MongoDB映射模型类
EN

Stack Overflow用户
提问于 2021-02-17 16:28:46
回答 3查看 55关注 0票数 0

我已经使用Node/MongoDB作为我的后端,一切都工作得很好,wrt请求和响应。

对于我的前端,我正在使用flutter构建一个移动应用程序,因此必须创建模型类来表示我的响应。回复示例:

代码语言:javascript
运行
复制
{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类:

代码语言:javascript
运行
复制
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映射的时候。下面是错误:

代码语言:javascript
运行
复制
I/flutter (19353): NoSuchMethodError: The method '[]' was called on null.
I/flutter (19353): Receiver: null
I/flutter (19353): Tried calling: []("_id")

有人知道我需要在UserModel类中做哪些更改吗?谢谢。

EN

Stack Overflow用户

发布于 2021-02-17 21:28:10

感谢所有试图帮助我理解这个问题的人。我的APIClient类中缺少一条返回语句,这确保了fromJSON方法中的映射中的所有值都为空。

更正如下:

代码语言:javascript
运行
复制
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);
    }
  }
}
票数 1
EN
查看全部 3 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66238251

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档