Reputation:
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");
}
}
}
}
Upvotes: 1
Views: 406
Reputation: 11
Implements Getx Service in AddProductController class. it will look like this:
class AddProductController extends GetxController implements GetxService{...... }
Upvotes: 0
Reputation: 1537
This error occurs when you use this operator (!)
.
you have to use using ternary operator example as :
void main() {
Students? student;
print(student?.name);
}
Upvotes: 0