简体   繁体   English

getx 导航错误 Null 检查运算符用于 null 值

[英]getx navigation Error Null check operator used on a null value

I need when the user click the button to add data to firebase, the snake bar pop up with a success message, then go back.我需要当用户单击按钮将数据添加到 firebase 时,蛇栏弹出一条成功消息,然后返回 go。 But there is NO navigation occurs.但是没有导航发生。

the error is occurs when i use navigator is:当我使用导航器时发生错误是:

Error Null check operator used on a null value错误 Null 检查运算符用于 null 值

the code is:代码是:

class AddProductController extends GetxController {
 
  addProduct() async {
    if ((addProductFormKey.currentState?.validate() ?? false) &&
        pickedPhoto != null) {
      String docID = FirebaseFirestore.instance.collection('products').doc().id;
      var url = "";
      try {
        UploadTask uploadTask = FirebaseStorage.instance
            .ref('users/products/$docID/')
            .putFile(pickedPhoto!);
        uploadTask.whenComplete(() async {
          url = await FirebaseStorage.instance
              .ref('users/products/$docID/')
              .getDownloadURL();
          await FirebaseFirestore.instance
              .collection("products")
              .doc(docID)
              .set({
            "imgUrl": url,
          }, SetOptions(merge: true));
          Get.snackbar(
            "Sucess",
            "Your Product Is Added",
            snackPosition: SnackPosition.BOTTOM,
          );
        }).catchError((onError) {
          print(onError);
        });

        return Get.toNamed(Routes.PRODUCTS); // => doees not work
      } catch (e) {
        print("\n Error $e \n");
      }
    }
  }
}

This error occurs when you use this operator (!) .使用此运算符(!)时会发生此错误。

you have to use using ternary operator example as:您必须使用三元运算符示例:

void main() {
    Students? student;
    print(student?.name);
   }

Implements Getx Service in AddProductController class.在 AddProductController class 中实现 Getx 服务。 it will look like this:它看起来像这样:

class AddProductController extends GetxController implements GetxService{...... } class AddProductController 扩展 GetxController 实现 GetxService{...... }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM