Home > database >  flutter FirebaseStorage onComplete
flutter FirebaseStorage onComplete

Time:04-16

 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'.

CodePudding user response:

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

CodePudding user response:

final ref = FirebaseStorage.instance
                          .ref("${DateTime.now().millisecondsSinceEpoch.toString()}.jpg");
                
    
var uploadEvent = ref.putFile(_image1!);
String imageUrl = await (await uploadEvent.whenComplete(() => null))
                      .ref
                      .getDownloadURL();
  • Related