简体   繁体   中英

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. But there is NO navigation occurs.

the error is occurs when i use navigator is:

Error Null check operator used on a null value

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. it will look like this:

class AddProductController extends GetxController implements GetxService{...... }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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