Lewis
Lewis

Reputation: 83

" task.future.then " keeps throwing an error in my Flutter code

This is my code -

    Future uploadImage() async {
var randomno = Random(25);
final StorageReference firebaseStorageRef = FirebaseStorage.instance
    .ref()
    .child('profilepics/${randomno.nextInt(5000).toString()}.jpg');
StorageUploadTask task = firebaseStorageRef.putFile(selectedImage);

task.future.then((value) {
  setState(() {
    userManagement
        .updateProfilePic(value.downloadUrl.toString())
        .then((val) {
      setState(() {
        profilePicUrl = value.downloadUrl.toString();
        isLoading = false;
      });
    });
  });
}).catchError((e) {
  print(e);
});
  }

This is the error it gives -

The getter 'future' isn't defined for the type 'StorageUploadTask'. Try importing the library that defines 'future', correcting the name to the name of an existing getter, or defining a getter or field named 'future'.

Although i have already imported dart:async and dart:io Please help, thanks

Upvotes: 1

Views: 208

Answers (2)

jaimin rana
jaimin rana

Reputation: 86

now 'onComplete' is also showing 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'.

Upvotes: 0

Peter Haddad
Peter Haddad

Reputation: 80914

StorageUploadTask doesnt have a property called future, you have to use onComplete the following:

final StorageReference firebaseStorageRef = FirebaseStorage.instance
    .ref()
    .child('profilepics/${randomno.nextInt(5000).toString()}.jpg');
StorageUploadTask task = firebaseStorageRef.putFile(selectedImage);
task.onComplete.then((value) {
  setState(() {
    userManagement
        .updateProfilePic(value.ref.getDownloadURL().toString())
        .then((val) {
      setState(() {
        profilePicUrl = value.ref.getDownloadURL().toString();
        isLoading = false;
      });
    });
  });

https://github.com/FirebaseExtended/flutterfire/blob/master/packages/firebase_storage/lib/src/upload_task.dart#L28

Upvotes: 1

Related Questions