ofFile method Null safety

String? ofFile(
  1. File file,
  2. {Iterable<MimeTypeDetector>? detectors}
)

Returns mime type of file based on its extension

Implementation

static String? ofFile(File file, {Iterable<MimeTypeDetector>? detectors}) {
  if (detectors != null) {
    final mimeType = detectors
        .map((e) => e(file))
        .firstWhere((mime) => mime != null, orElse: () => null);
    if (mimeType != null) {
      return mimeType;
    }
  }

  String fileExtension = p.extension(file.path);

  if (fileExtension.startsWith('.'))
    fileExtension = fileExtension.substring(1);

  if (fileExtension.length == 0) {
    return null;
  }

  if (fromFileExtension.containsKey(fileExtension)) {
    return fromFileExtension[fileExtension];
  }

  return null;
}