1

So I already read about the topic but I simply didn't understand the solutions on stack.

I came up with this code:

Im saving a url looking like this:

final String myDataUrl = file.url;
print(myDataUrl);

blob:http://localhost:51947/2952a3b1-db6a-4882-a42a-8e1bf0a0ad73

& then Im trying to add it into Firebase Storage with the putString operator, that I guessed that suited me best while reading the Documentation. I thought that I have a Url and therefore should be able to upload it like this:

      FirebaseStorage.instance
      .ref()
      .child("bla")
      .putString(myDataUrl, format: PutStringFormat.dataUrl);

But it doesn't work, it says that:

Error: Invalid argument (uri): Scheme must be 'data': Instance of '_Uri'

So Im guessing that it somehow can't format my url to one that is accepted.

What can I do different to upload a blob successfully to firebase Storage?

-----------------Answer----------------------

Answer in the comment of the answer.

You have to convert your Blob to a Uint8List & upload it like:

 Future<Uint8List> fileConverter() async {
final reader = html.FileReader();
reader.readAsArrayBuffer(file!);
await reader.onLoad.first;
return reader.result as Uint8List;
 }

and then put it into your Storage:

  Future uploadFile(String uid) async {
if (file == null) return;
final path = "nachweise/$uid";
Uint8List fileConverted = await fileConverter();
try {
  FirebaseStorage.instance
      .ref()
      .child(path)
      .putData(fileConverted)
      .then((bla) => print("sucess"));
} on FirebaseException catch (e) {
  return null;
}
}

1 Answer 1

2

The Firebase Storage SDKs can upload local data as either a File, an array of bytes, or a base-64 encoded string. The only URLs it accepts are so-called data URLs, which start with data:// and contain the complete data of the object. They cannot upload data directly from URLs that you more commonly see, such as http:// or https://.

You'll need to first download the data from that URL to the local device, and then upload it from there.

3
  • I have the blob object, how can I add it? The URL thing was a try to upload the blob through an URL because I didn’t know how to add the blob itself
    – Nikita
    Aug 20, 2022 at 18:31
  • It looks like on Flutter it needs to be a Uint8List: firebase.google.com/docs/storage/flutter/… Aug 20, 2022 at 18:43
  • Thank you so much, you really saved my ass here Up to today I didn't even know what a blob or Uint8List was What's more out there?! :P
    – Nikita
    Aug 20, 2022 at 20:31

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.