saveBytes static method

Future<String> saveBytes(
  1. XFile imageFile,
  2. String path, {
  3. String? extensionFormat,
})

Supports the extension format, if not set, will be set to application/octet-stream (bytes)

Implementation

static Future<String> saveBytes(XFile imageFile, String path,
    {String? extensionFormat}) async {
  var byteData = await imageFile.readAsBytes();
  final name = imageFile.name.split('.').first;
  String fileName =
      '$name-${DateTime.now().millisecondsSinceEpoch.toString()}';
  Reference reference =
      FirebaseStorage.instance.ref(path + fileName + (extensionFormat ?? ""));
  UploadTask uploadTask = reference.putData(byteData.buffer.asUint8List());
  if (instance._showProgress) {
    if (instance._context == null) {
      throw Exception("Must set context if showProgress is true");
    }
    if (instance._showDataUploadProgress != null) {
      await instance._showDataUploadProgress!(uploadTask);
    } else {
      await FireUploader()
          .showDataUploadProgress(instance._context!, uploadTask);
    }
  }
  TaskSnapshot storageTaskSnapshot = await uploadTask.whenComplete(() {});
  String link = await storageTaskSnapshot.ref.getDownloadURL();
  return link;
}