'flutter FirebaseStorage onComplete
void validateAndUpload() async {
if (_formKey.currentState.validate()) {
setState(() => isLoading = true);
if (_image1 != null) {
if (selectedSizes.isNotEmpty) {
String imageUrl1;
final FirebaseStorage storage = FirebaseStorage.instance;
final String picture1 =
"${DateTime.now().millisecondsSinceEpoch.toString()}.jpg";
StorageUploadTask task1 =
storage.ref().child(picture1).putFile(_image1);
task1.onComplete.then((snapshot1) async {
imageUrl1 = await snapshot1.ref.getDownloadURL();
_productServices.uploadProduct(
productName: productNameController.text,
brandName: _currentBrand,
details: detailController.text,
category: _currentCategory,
quantity: int.parse(quantityController.text),
size: selectedSizes,
picture: imageUrl1,
feature: feature,
sale: sale,
price: double.parse(priceController.text));
_formKey.currentState.reset();
The getter 'onComplete' isn't defined for the type 'UploadTask'. (Documentation) Try importing the library that defines 'onComplete', correcting the name to the name of an existing getter, or defining a getter or field named 'onComplete'.
Solution 1:[1]
That error seems correct. Did you mean whenComplete?
I typically prefer to simply await the task though:
var ref = storage.ref().child(picture1);
await ref.putFile(_image1);
imageUrl1 = await ref.getDownloadURL();
...
Solution 2:[2]
final ref = FirebaseStorage.instance
.ref("${DateTime.now().millisecondsSinceEpoch.toString()}.jpg");
var uploadEvent = ref.putFile(_image1!);
String imageUrl = await (await uploadEvent.whenComplete(() => null))
.ref
.getDownloadURL();
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Frank van Puffelen |
| Solution 2 | MD. Saffan Alvy |
