Flutter 颤振中获取错误:-在null上调用了getter
我正在尝试获取用户选择的maxDistance并继续获取此错误。这是从获取数据到显示数据的完整代码。在云Firestore中,数据也被保存,但我不知道为什么我不能在这里显示这些数据。请帮帮我。谢谢。我得到了这个错误:-Flutter 颤振中获取错误:-在null上调用了getter,flutter,dart,google-cloud-firestore,flutter-layout,Flutter,Dart,Google Cloud Firestore,Flutter Layout,我正在尝试获取用户选择的maxDistance并继续获取此错误。这是从获取数据到显示数据的完整代码。在云Firestore中,数据也被保存,但我不知道为什么我不能在这里显示这些数据。请帮帮我。谢谢。我得到了这个错误:- The getter 'maxDistance' was called on null. Receiver: null Tried calling: maxDistance 这是我将其命名为的屏幕:- class More extends StatefulWidge
The getter 'maxDistance' was called on null.
Receiver: null
Tried calling: maxDistance
这是我将其命名为的屏幕:-
class More extends StatefulWidget{
final CreateAccountData currentUser;
More({this.currentUser,}) ;
@override
_MoreState createState() => new _MoreState();
}
class _MoreState extends State<More>
with SingleTickerProviderStateMixin{
Map<String, dynamic> changeValues = {};
final auth = FirebaseAuth.instance;
int distance;
String currentuser;
int freeR;
int paidR;
@override
void initState() {
super.initState();
freeR = 500;
paidR = 500;
setState(() {
if (widget.currentUser.maxDistance > freeR) {
widget.currentUser.maxDistance = freeR.round();
changeValues.addAll({'maximum_distance': freeR.round()});
} else if (
widget.currentUser.maxDistance >= paidR) {
widget.currentUser.maxDistance = paidR.round();
changeValues.addAll({'maximum_distance': paidR.round()});
}
distance = widget.currentUser.maxDistance.round();
});
}
@override
void dispose() {
super.dispose();
if (changeValues.length > 0) {
updateData();
}
}
Future updateData() async {
FirebaseFirestore.instance
.collection("users")
.doc(widget.currentUser.uid)
.set(changeValues,SetOptions( merge: true));
}
这是我获取数据的模型类:-
class CreateAccountData {
final String name, email,DOB,age,uid,username, countryCode ,
address,phone, password,profilepic,showGender,gender;
final Map coordinates,editInfo,ageRange;
final List hobbies ;
int maxDistance;
var distanceBW;
CreateAccountData({this.name,this.email,this.DOB,this.age,
this.username,this.uid,this.countryCode,this.address,
this.phone, this.password,this.profilepic,this.coordinates,
this.hobbies,this.gender,this.showGender,this.ageRange,this.maxDistance,
this.editInfo,this.distanceBW});
factory CreateAccountData.fromJson(Map<String, dynamic> json) {
return CreateAccountData(
name: json['name'],
email: json['email'],
age:json['age'],
DOB:json['DOB'],
gender: json['gender'],
hobbies: json['hobbies']['list'],
address: json['location']['address'],
coordinates: json['location'],
username:json['username'],
uid:json['uid'],
editInfo:json['editInfo'],
maxDistance:json['maximum_distance'],
showGender:json['showGender'],
phone: json['phone'],
profilepic : json['profilepic']
);
}
factory CreateAccountData.fromDocument(DocumentSnapshot doc){
return CreateAccountData(
uid: doc['uid'],
phone: doc['phone'],
name: doc['name'],
editInfo: doc['editInfo'],
ageRange: doc['age_range'],
showGender: doc['showGender'],
maxDistance: doc['maximum_distance'],
age:doc['age'],
address: doc['location']['address'],
coordinates: doc['location'],
);
}
Map<String, dynamic> toMap() {
return {
'name':name,
'email': email,
'age': age,
'username':username,
'DOB':DOB,
'gender':gender,
'uid':uid,
'showGender':showGender,
'address':address,
'editInfo':editInfo,
'hobbies':hobbies,
'location':coordinates,
'maximum_distance':maxDistance,
'phone' : phone,
'profilepic':profilepic,
};
}
}
请用调试点检查此行
if (widget.currentUser.maxDistance > freeR) {
我认为currentUser在这里是空的,如果是,这可能会解决您的问题
将此中的第二行更改为
class More extends StatefulWidget{
final CreateAccountData currentUser;
More({this.currentUser,}) ;
对此
CreateAccountData currentUser = CreateAccountData();
如果它没有解决您的问题,请确保您正在将currentUser发送到此小部件调试currentUser。我认为您没有将currentUser传递到此页面
widget?.currentUser?.maxDistance!=空
像这样检查
setState(() {
if(widget?.currentUser?.maxDistance != null){
if (widget.currentUser.maxDistance > freeR) {
widget.currentUser.maxDistance = freeR.round();
changeValues.addAll({'maximum_distance': freeR.round()});
} else if (
widget.currentUser.maxDistance >= paidR) {
widget.currentUser.maxDistance = paidR.round();
changeValues.addAll({'maximum_distance': paidR.round()});
}
distance = widget.currentUser.maxDistance.round();
} else {
print('currentUser is null ${widget?.currentUser?.maxDistance}')
}
});
是的,我认为currentUser不会访问这个小部件。我该怎么做?我需要在哪里做什么更改?你是否按照我说的检查了?因为最终CreateAccountData currentUser;这条线会制造很多问题。。。但是如果你想确保返回到你正在调用的小部件More widget。。。看看你有没有发什么东西?如果是,请使用调试点检查它,以确保它不是空的或Null如果我将名称“CreateAccountData”更改为“User”,则会出现另一个错误:-没有为类型“User”定义getter“maxDistance”。是的,我已尝试此操作:-CreateAccountData currentUser=CreateAccountData();它不起作用。好的,那么请检查您是否正在向此小部件发送任何内容。您可以发布导航到
更多
的屏幕的代码吗?您需要将当前用户
传递到更多
。已更新,请检查,可能是我传递错误,请指导我。是的,没有打印内容。我怎么做?好的。这意味着,widget?.currentUser?.maxDistance!=null
为真。是吗?请帮帮我。我可以在任何桌子上分享我的屏幕吗。。陷入困境…你从哪里得到这个错误的?`对null调用了getter'maxDistance'。‘好的,当然@GAGANSINGH
class More extends StatefulWidget{
final CreateAccountData currentUser;
More({this.currentUser,}) ;
CreateAccountData currentUser = CreateAccountData();
setState(() {
if(widget?.currentUser?.maxDistance != null){
if (widget.currentUser.maxDistance > freeR) {
widget.currentUser.maxDistance = freeR.round();
changeValues.addAll({'maximum_distance': freeR.round()});
} else if (
widget.currentUser.maxDistance >= paidR) {
widget.currentUser.maxDistance = paidR.round();
changeValues.addAll({'maximum_distance': paidR.round()});
}
distance = widget.currentUser.maxDistance.round();
} else {
print('currentUser is null ${widget?.currentUser?.maxDistance}')
}
});