getFileName method

String getFileName({
  1. bool withMimeType = false,
  2. bool appendMimeType = true,
})

Extracts the file name from a path, URL, or asset.

If withMimeType is true, it returns the name with the MIME type. If appendMimeType is true, it will return the name with .mimeType

Implementation

String getFileName({bool withMimeType = false, bool appendMimeType = true}) {
  // Extract file name from path or URL
  String fileName = split('/').last.split('\\').last;

  if (withMimeType) {
    // Determine MIME type
    String mimeType = fileName.mimeType;
    return appendMimeType ? "$fileName.$mimeType" : "$fileName ($mimeType)";
  }

  return fileName;
}