flutter - 在 null 上调用了方法“map”。扑
问题描述
我正在尝试从 Json 获取用户,但不知何故我收到错误消息
the method 'map' was called on null. flutter
我不知道为什么,这是我的模型代码:
class User {
final String id;
final String email;
final String username;
List<FollowUserModel> following = [];
List<FollowUserModel> followers = [];
User({this.id,this.email,this.followers,this.following, this.username});
factory User.fromJSON(Map<String, dynamic> jsonMap) {
return User(
id: jsonMap['id'] as String,
email: jsonMap['email'] as String,
username: jsonMap['username'] as String,
following: jsonMap["following_set"] != null ? List<FollowUserModel>.from( jsonMap["followiing_set"].map((x) => FollowUserModel.fromJSON(x))) :[],
followers: jsonMap["followers_set"] != null ? List<FollowUserModel>.from( jsonMap["followers_set"].map((x) => FollowUserModel.fromJSON(x))) :[],
);
}
}
class FollowUserModel {
final String id;
final User author;
final User profile;
FollowUserModel({this.id,this.author,this.profile});
factory FollowUserModel.fromJSON(Map<String, dynamic> jsonMap) {
return FollowUserModel(
id: jsonMap['id'] as String,
author: jsonMap['author'] as User,
profile: jsonMap['profile'] as User,
);
}
}
我之前将数组声明为 [],因为我希望返回一些空值,但仍然出现错误。有人知道原因吗?
更新 我知道我打错了,但错误仍然存在。
解决方案
你打错了,而不是 'following_set' 和 'followers_set' 你写的是 'followiing_set' 和 'followrse_set':
....
following: jsonMap["following_set"] != null ? List<FollowUserModel>.from( jsonMap["followiing_set"].map((x) => FollowUserModel.fromJSON(x))) :[],
followers: jsonMap["followers_set"] != null ? List<FollowUserModel>.from( jsonMap["followrse_set"].map((x) => FollowUserModel.fromJSON(x))) :[],
....
推荐阅读
- python - How to download Glut for PyOpenGL, error Attempt to call an undefined function glutInit, 尝试了很多解决方案,都不行
- java - 如何解决 Unity android 插件中的 java.lang.NoClassDefFoundError
- python - 计算 3-D 卷曲的最快算法
- xamarin.forms - 如何在两个 ListView 控件之间拖放项目?
- excel - Power BI - 采用多列年份和值列并合并为 2 列
- sql-server - Golang 连接到 SQL Server 错误 - “TLS 握手失败:无法读取握手包:EOF”
- excel - 使用 VBA 链接有数据的电子表格
- angular - 在路由中传递隐藏的多个参数
- html - 如何左对齐 Vimeo 嵌入
- date - 为什么提交表单后我的日期公式消失了?