Flutter: Get the filename of a File

I thought this would be pretty straight-forward, but can't seem to get this. I have a File file and it has a path file.path which spits out something like /storage/emulated/0/Android/data/my_app/files/Pictures/ca04f332.png but I can't seem to find anything to get just ca04f332.png.


Solution 1:

You can use the basename function from the dart path library:

import 'package:path/path.dart';

File file = new File("/dir1/dir2/file.ext");
String basename = basename(file.path);
# file.ext

Solution 2:

File file = new File("/storage/emulated/0/Android/data/my_app/files/Pictures/ca04f332.png"); 
String fileName = file.path.split('/').last;

print(fileName);

output = ca04f332.png