我遇到了这个错误,我不知道如何解决,请帮助我:
The method 'where' was called on null.
Receiver: null
Tried calling: where(Closure: (UserModel) => bool)
这是错误的来源:
List 属于 UserModel 类,如下所示
List<UserModel> userList;
final List<UserModel> suggestionList = query.isEmpty
?[]
: userList.where((UserModel user) {
String _getUsername = user.username.toLowerCase();
String _query = query.toLowerCase();
String _getName = user.name.toLowerCase();
bool matchesUsername = _getUsername.contains(_query);
bool matchesName = _getName.contains(_query);
return (matchesUsername || matchesName);
}).toList();
这是 userList
调用的类:
这是开始调用的模型类。
class UserModel {
String uid;
String name;
String email;
String username;
String status;
int state;
String profilePhoto;
UserModel({
this.uid,
this.name,
this.email,
this.username,
this.status,
this.state,
this.profilePhoto,
});
Map toMap(UserModel user) {
var data = Map<String, dynamic>();
data['uid'] = user.uid;
data['name'] = user.name;
data['email'] = user.email;
data['username'] = user.username;
data["status"] = user.status;
data["state"] = user.state;
data["profile_photo"] = user.profilePhoto;
return data;
}
UserModel.fromMap(Map<String, dynamic> mapData) {
this.uid = mapData['uid'];
this.name = mapData['name'];
this.email = mapData['email'];
this.username = mapData['username'];
this.status = mapData['status'];
this.state = mapData['state'];
this.profilePhoto = mapData['profile_photo'];
}
}
答案 0 :(得分:1)
“userList”变量未初始化。您可以做的最简单的修复是在声明它时使用空列表对其进行初始化。
List<UserModel> userList = <UserModel>[];