Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 147 Vote(s) - 3.69 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Flutter: Get the filename of a File

#1
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`.
Reply

#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
Reply

#3
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]

Reply

#4
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>);
}

Reply

#5
#### 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
```
Reply

#6
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

Reply

#7
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]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through