0

I'm trying to upload an image to firebase storage but when I called the function, await is not executed to get the url. What am I missing in this?

Looking at this other topic I've got that the problem may be the "then", but how can I set the code to await the url?

Async/Await/then in Dart/Flutter

Future < String > uploadImage(File imageFile) async {
  String _imageUrl;
  StorageReference ref =
    FirebaseStorage.instance.ref().child(firebaseUser.uid.toString());

  await(ref.putFile(imageFile).onComplete.then((val) {
    val.ref.getDownloadURL().then((val) {
      _imageUrl = val;
      print(val);
      print("urlupload");
    });
  }));

  print(_imageUrl);
  print("urlnoupload");

  return _imageUrl;

}

Thanks!

terahertz
  • 2,915
  • 1
  • 21
  • 33
noworries
  • 3
  • 2

1 Answers1

0

you had async/await when you didn't need it because you were capturing value in the then function

Future<String> uploadImage(File imageFile) async {
  String _imageUrl;
  StorageReference ref = FirebaseStorage.instance.ref().child(firebaseUser.uid.toString());
  return ref.putFile(imageFile).onComplete.then((val) {
      return val.ref.getDownloadURL()
  }).then((_imageUrl) {
      return _imageUrl;
  });
},
Aaron Saunders
  • 33,180
  • 5
  • 60
  • 80
  • It worked Aaron, thanks, I think I get the "then" now. Just one more thing, why you need to set a return 3 times? – noworries Jul 20 '19 at 02:51