The Cloud FireStore supports Bytes datatype, but failed to give an example of how to setting it, i am trying to upload a thumbnail image as bytes to the FireStore, in Flutter. Can someone give an example how to do it?
File imageFile;
final databaseReference = Firestore.instance;
await databaseReference
.collection("Users")
.document('${user.strId}')
.setData({'thumbnailPhoto': ???}); // how to convert imageFile to Bytes?
EDIT
I think both of the answers posted by Richard Heap and Jay Vasan work, using blob
or base64Encode
actually give the same value in FireStore under key thumbnailPhoto
. I decide to give the bounty to Richard since it hides the details of the internal binary coding.
Cloud Firestore raw bytes is represented by Blob
in Dart, so use:
.setData({'thumbnailPhoto', Blob(await file.readAsBytes())});
When you read this back from Cloud Firestore you'll get back a Blob
. Access its bytes with blob.bytes
. For example, to write to a file use:
await file.writeAsBytes(theBlob.bytes); // overwrite or create the file
In this case, you can convert the image to base64 string.
open image as:
var imageFile = File(imagePath);
convert to base64:
String base64Image = base64Encode(imageFile.readAsBytesSync());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With