Home > OS >  How to get the url from Firebase Storage file?
How to get the url from Firebase Storage file?

Time:01-19

I want to retrieve the URL of a Uint8List image from firebase storage. But I am not able to do that. It prints a snapshot, not the url. Please help.

my code:

 void _storeSignature() async {
        try {
          final  Uint8List? signature = await exportSignature();
    
          final FirebaseStorage? storage = FirebaseStorage.instance;
          final String? pictureUrl =
              "signature/${DateTime.now().millisecondsSinceEpoch.toString()}.png";
    
          final ref = storage!.ref().child(pictureUrl!);
          final uploadTask = ref.putData(signature!);
          final downurl = await uploadTask.whenComplete(() {
            return ref.getDownloadURL();
          });
          final url = downurl.toString();
    
          print(url);
    
        } catch (e) {
          rethrow;
        }
      }

CodePudding user response:

You have to await for the URL when calling ref.getDownloadURL()

change

return ref.getDownloadURL();

into

return await ref.getDownloadURL();

CodePudding user response:

The UploadTask class extends Future, so you can also use await there already:

await ref.putData(signature!);
final downurl = await ref.getDownloadURL();
final url = downurl.toString();
  •  Tags:  
  • Related