getName method

String getName([
  1. bool includeExtension = false
])

Returns the name of the file, without any directory in the string.

var file = File('/root/directory/file.mp3');
file.getName(); // 'file'
file.getName(true); // 'file.mp3'

Implementation

String getName([bool includeExtension = false]) {
  var separator = Platform.pathSeparator;
  var fileNameWithExtension = path.split(separator).last;
  if (includeExtension) {
    return fileNameWithExtension;
  }
  var parts = fileNameWithExtension.split('.');
  parts.removeLast();
  return parts.join('.');
}