How to Get Filename of a File In Flutter ?

· 2 min read
How to Get Filename of a File In Flutter
How to Get Filename of a File In Flutter

Generally when a user uploads a file or image from Phone Camera or Phone Gallery user will get a path to File So in this article we will go through How to Get Filename of a File In Flutter?

How to Get Filename a File In Flutter?

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

Consider a code snippet like the below:

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


Since Dart Version 2.6 has been announced and it’s available for flutter version 1.12 and higher, You can use extension methods. It will provide a more readable and global solution to this problem.

file_extensions.dart will have a code snippet like the below:

import 'dart:io';

extension FileExtention on FileSystemEntity{
  String get name {
    return this?.path?.split("/")?.last;

and name getter is added to all the file objects. You can simply just call name on any file.

main() {
  File file = new File("/dev/dart/work/hello/app.dart");

You have to import your extension manually wherever you need that. Just make sure the extension file is imported:

import 'package:<your_extention_path>/file_extentions.dart';


In this article, we have been through How to Get Filename of a File In Flutter?

Thanks for Reading !!! Keep Learning !!!

Drop us your valuable feedback for the same. is our portal Platform dedicated to Flutter Technology and Flutter Developers. The portal is full of cool resources from Flutter like Flutter Widget GuideFlutter ProjectsCode libs and etc. is one of the most popular online portal dedicated to Flutter Technology and daily thousands of unique visitors come to this portal to enhance their knowledge on Flutter.

Leave a Reply