Flutter - > UploadTask method OnComplete does not exists

8,641

Solution 1

I think you don't need onComplete anymore, and can just await the Future that's returned from putFile.

For the latest code samples, always check the FlutterFire documentation, for example: uploading a file.

Solution 2

Thank you very much it works. This is the final code

  print(sampleImage);
  UploadTask uploadTask =
      postImageRef.child(timeKey.toString() + ".jpg").putFile(sampleImage);
  print('aaa');
  print(uploadTask);
  var imageUrl = await (await uploadTask).ref.getDownloadURL();
  url = imageUrl.toString();
  print(url);
  // Guardar el post en la bbdd

Solution 3

Use the TaskSnapshot class instead. You don't need onComplete.

Example:

TaskSnapshot taskSnapshot = await uploadTask;
Share:
8,641
Joan Codinach Ortiz
Author by

Joan Codinach Ortiz

Updated on December 25, 2022

Comments

  • Joan Codinach Ortiz
    Joan Codinach Ortiz over 1 year

    I see a lot of exercises that put onComplete but with my code this is the error

    The getter 'onComplete' isn't defined for the type 'UploadTask'. 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'

    WHY ??

    void uploadImage() async {
        if (safeNeuralNetwork()) {
          //Subir imagen a firebase storage
          final Reference postImageRef =
              FirebaseStorage.instance.ref().child("Post Images");
          var timeKey = DateTime.now();
        
          print(sampleImage);
          UploadTask uploadTask =
              postImageRef.child(timeKey.toString() + ".jpg").putFile(sampleImage);
          var imageUrl =
              await (await uploadTask.onComplete).ref.getDownloadURL();
          url = imageUrl.toString();
          print(url);
          // Guardar el post en la bbdd
          saveToDatabase(url);
          //Regresar en Home
          Navigator.pop(context);
        }
      }