在颤振中出错:-在 null 上调用了 getter

时间:2021-05-22 07:53:34

标签: flutter dart google-cloud-firestore flutter-layout

我正在尝试获取用户选择的 maxDistance 并不断收到此错误。这是从我获取数据到显示位置的完整代码。在 Cloud Firestore 中也保存了数据,但我不知道为什么我无法在此处显示该数据。请帮帮我。谢谢。我收到此错误:-

 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));

 }

这是我要显示的地方:-

     Padding(
              padding: const EdgeInsets.all(10.0),
               child: Card(
               child: Padding(
                padding: const EdgeInsets.all(8.0),
                 child: ListTile(
                  title: Text(
                  "Maximum distance",
                   style: TextStyle(
                      fontSize: 18,
                                color: mRed,
                                fontWeight: FontWeight.w500),
                          ),
                          trailing: Text(
                            "$distance Km.",
                            style: TextStyle(fontSize: 16),
                          ),
                          subtitle: Slider(
                               value: distance.toDouble(),
                              inactiveColor: Colors.blueGrey,
                              min: 1.0,
                              max: 500,
                              activeColor: mRed,
                              onChanged: (val) {
                                changeValues
                                    .addAll({'maximum_distance': val.round()});
                                setState(() {
                                  distance = val.round();
                                });
                              }),
                        ),
                      ),
                    ),
                  ),

这是我从中获取数据的模型类:-

      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,
};
 }

 }

用户切换到其他标签的底部导航屏幕:-

       class BottomNav extends StatefulWidget {
       @override
     _BottomNavState createState() => _BottomNavState();
  }

  class _BottomNavState  extends State<BottomNav> {
   int _index = 0;
    List<Widget> _items = [
   Home(),
  UserProfile(FirebaseAuth.instance.currentUser,imageList:    [],currentIndex:0),
  Notifications(),
  Chat(),
  More(),
  ];  

尝试在 More() 中传递 currentUser:- 然后它给了我这个:-

    The argument type 'Type' can't be assigned to the parameter type 'CreateAccountData.
     Undefined name 'currentUser'.

2 个答案:

答案 0 :(得分:0)

请用调试点检查这一行

if (widget.currentUser.maxDistance > freeR) {

我认为 currentUser 在这里为 null 如果是的话这可能会解决您的问题

把第二行改成

  class More extends StatefulWidget{
      final CreateAccountData currentUser;
      More({this.currentUser,}) ;

到这里

CreateAccountData currentUser = CreateAccountData();

如果它没有解决您的问题,请确保您将 currentUser 发送到此小部件

答案 1 :(得分:0)

调试当前用户。我认为您没有将 currentUser 传递到此页面。

widget?.currentUser?.maxDistance != null

像这样检查

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}')
 }
});