0
Future<String> uploadFile(String filePath) async {
    File file = File(filePath);
    final fileName = basename(filePath);
    try {
      firebase_storage.TaskSnapshot taskSnapshot = await firebase_storage.FirebaseStorage.instance
          .ref("news/$fileName")
          .putFile(file);
      await taskSnapshot.ref.getDownloadURL().then(
            (value) {
              print("Done: $value");
              return value;
            }

      );
      
      return "123";
    } on firebase_core.FirebaseException catch (e) {
      print(e);
      return "#";
    }
  }

uploadFile returns me 123 since the value is get printed after that, How do I return value from this function before returning "123"??

1
  • 1
    return await taskSnapshot.ref.... and of course remove return "123"; Commented Aug 14, 2021 at 6:28

1 Answer 1

1

Instead of using .then(...), You need to wait for the getDownloadUrl() task and then return it's value.

Future<String> uploadFile(String filePath) async {
  File file = File(filePath);
  final fileName = basename(filePath);
  try {
    firebase_storage.TaskSnapshot taskSnapshot = await firebase_storage
        .FirebaseStorage.instance
        .ref("news/$fileName")
        .putFile(file);
    final url = await taskSnapshot.ref.getDownloadURL();
    return url;
  } on firebase_storage.FirebaseException catch (e) {
    print(e);
    return "#";
  }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.