Firebase Storage await uploadTask.onComplete outdated

Viewed 2198

Im using this code. The error message is the following: error: The getter 'onComplete' isn't defined for the type 'UploadTask'. (undefined_getter at [chatneu] lib/Screens/HomeScreen.dart:289)

  Future uploadFile() async {
    try {
      pr = new ProgressDialog(context);

      await ImagePicker().getImage(
          source: ImageSource.gallery).then((image) {
        setState(() {
          _image = image as File;
          //klammern weg bei ImagePicker und .getImage zu Pickimage
        });
      });
      await pr.show();

      Reference storageReference = FirebaseStorage.instance.ref().child(
          '${loggedInUser.uid}/UserProfille/${Path.basename(_image.path)}');
      UploadTask uploadTask = storageReference.putFile(_image);
      await uploadTask.onComplete;
      print('File Uploaded');
      storageReference.getDownloadURL().then((fileURL) {
        setState(() {
          FirebaseFirestore.instance.collection('Users').doc(loggedInUser.uid);
          Map<String, String> data = {
            'photoUrl': fileURL,
          };
2 Answers

I'm not sure where you got the idea that there is a method called onComplete on the UploadTask. If you follow the example code in the documentation, you will see that you just await the UploadTask directly:

await storageReference.putFile(_image);
storageReference.getDownloadURL().then(...);

You might also want to review the docs on handling tasks.

onComplete does not have any more but there is different method is whenComplete may replace what you need.

await ref.putFile(image).whenComplete(() => doSomething());
Related