![]() |
Flutter: Get the filename of a File - Printable Version +- 0Day Forums (https://zeroday.vip) +-- Forum: Coding (https://zeroday.vip/Forum-Coding) +--- Forum: Flutter & Dart (https://zeroday.vip/Forum-Flutter-Dart) +--- Thread: Flutter: Get the filename of a File (/Thread-Flutter-Get-the-filename-of-a-File) |
Flutter: Get the filename of a File - catricecatrina630 - 07-21-2023 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`. RE: Flutter: Get the filename of a File - glade562 - 07-21-2023 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 RE: Flutter: Get the filename of a File - islandless928643 - 07-21-2023 You can use the [`basename`][1] function from the dart [path library][2]: import 'package:path/path.dart'; File file = new File("/dir1/dir2/file.ext"); String basename = basename(file.path); # file.ext [1]: [To see links please register here] [2]:[To see links please register here] RE: Flutter: Get the filename of a File - Mrkelsi1 - 07-21-2023 Easy way to get name or any other file handling operations.I recommend to use this plugin : [To see links please register here] main() { String filename= FileSupport().getFileNameWithoutExtension(<File Object>); } RE: Flutter: Get the filename of a File - slattedzsbzyec - 07-21-2023 #### Direct way: ```dart File file = File('/foo/bar/baz/my_image.jpg'); String fileName = file.path.split(Platform.pathSeparator).last; // my_image.jpg ``` --- #### Using an extension: **1. Create an extension:** ```dart extension FileEx on File { String get name => path.split(Platform.pathSeparator).last; } ``` **2. Usage:** ```dart File file = File('/foo/bar/baz/my_image.jpg'); String fileName = file.name; // my_image.jpg ``` RE: Flutter: Get the filename of a File - harmfully869654 - 07-21-2023 You cane use extension method **No need to use any package** **Create Method like this** extension FileNameExtension on File { String getFileName() { String fileName = path.split('/').last; return fileName; } } **implement** File file = File("yourpath/example.pdf"); file.getFileName() // result => example.pdf RE: Flutter: Get the filename of a File - djebel216 - 07-21-2023 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` : ``` import 'dart:io'; extension FileExtention on FileSystemEntity{ String get name { return this?.path?.split(Platform.pathSeparator)?.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"); print(file.name); } ``` Read the [document][1] for more information. **Note:** Since `extension` is a new feature, it's not fully integrated into IDEs yet and it may not be recognized automatically. 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'; ``` [1]: [To see links please register here] |