filenamesflutter

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

  • 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