uploadImageWeb function

Future<List<String>> uploadImageWeb({
  1. required String storagePath,
  2. required List<Uint8List> imagebyteList,
  3. UploadImageFormat format = UploadImageFormat.webp,
})

Implementation

Future<List<String>> uploadImageWeb({
  required String storagePath,
  required List<Uint8List> imagebyteList,
  UploadImageFormat format = UploadImageFormat.webp,
}) async {
  try {
    // Upload compressed images to Firebase Storage
    final uploadTask = <UploadTask>[];
    for (final element in imagebyteList) {
      final ref = FirebaseStorage.instance
          .ref()
          .child(storagePath)
          .child('${DateTime.now().microsecondsSinceEpoch}.${format.name}');
      uploadTask.add(
        ref.putData(
          element,
          SettableMetadata(contentType: 'image/${format.name}'),
        ),
      );
    }
    final taskSnapshotList = await Future.wait(uploadTask);

    // Retrieve download URLs from the uploaded images
    final futureDownloadURL = taskSnapshotList.map(
      (e) => e.ref.getDownloadURL(),
    );
    final urlList = await Future.wait(futureDownloadURL);
    return urlList;
  } on FirebaseException catch (e) {
    throw Exception('firebase_storage_error: ${e.message}');
  } catch (e) {
    throw Exception('firebase_storage_error: $e');
  }
}