Flutter: Get the filename of a File

Jus10 picture Jus10 · May 21, 2018 · Viewed 23.7k times · Source

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.

Answer

jspcal picture jspcal · May 21, 2018

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