uploadMedia method

Future<String?> uploadMedia(
  1. File file,
  2. String path
)

Upload a media file (image/video) and return its download URL

Implementation

Future<String?> uploadMedia(File file, String path) async {
  try {
    // Create a reference with a unique file name
    Reference ref = _storage
        .ref()
        .child('$path/${DateTime.now().millisecondsSinceEpoch}');

    // Upload file
    UploadTask uploadTask = ref.putFile(file);
    TaskSnapshot snapshot = await uploadTask;

    // Get download URL
    return await snapshot.ref.getDownloadURL();
  } catch (e) {
    print('Error uploading media: $e');
    return null;
  }
}