saveObject method

  1. @override
Future<String> saveObject(
  1. String path,
  2. dynamic byteData, {
  3. String? extensionFormat,
  4. String? fileName,
  5. bool showProgress = false,
  6. BuildContext? context,
})

Uploads the selected bytes and returns file link Supports the extension format, if not set, will be set to application/octet-stream (bytes)

  • bool showProgress: if true, shows upload progress indicator and MUST set the context

  • BuildContext context: if showProgress is true, MUST set the context

Implementation

@override
Future<String> saveObject(String path, byteData,
    {String? extensionFormat,
    String? fileName,
    bool showProgress = false,
    BuildContext? context}) async {
  String fileNameAux =
      fileName ?? DateTime.now().millisecondsSinceEpoch.toString();
  Reference reference = FirebaseStorage.instance
      .ref(path + fileNameAux + (extensionFormat ?? ""));
  UploadTask? uploadTask;
  switch (byteData.runtimeType) {
    case String:
      uploadTask = reference.putString(byteData as String);
      break;
    case Uint8List:
      uploadTask = reference.putData(byteData as Uint8List);
      break;
    default:
      uploadTask = reference.putData(byteData as Uint8List);
  }
  if (showProgress) {
    if (context != null) {
      await showDataUploadProgress(context, uploadTask);
    } else {
      if (kDebugMode) {
        throw 'Must set context if show progress is true';
      }
    }
  }
  return await getDownloadUrl(uploadTask);
}