THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.
The post is about how to get the name of the file in dart programming.
There are multiple ways to get the name of the file
path/path.dart
package into your code.Path package provides basename and basenameWithoutExtension properties to get name and extension.
First, Create a file object.
Pass file.path
to those methods.
Here is an example
import 'dart:io';
import 'package:path/path.dart';
void main() {
File file = new File("/abc.txt");
String name = basename(file.path);
print(name);
String namewithoutExtension = basenameWithoutExtension(file.path);
print(namewithoutExtension);
}
Output:
abc.txt
abc
The second way, using File.uri.pathSegments.last
import 'dart:io';
import 'package:path/path.dart';
void main() {
File file = new File("/abc.txt");
String name = file.uri.pathSegments.last;
print(name);//abc.txt
}
Third-way using Path class from dart:io package Create File object, pass file.path that gets the path of the file to Path class.
Here is an example
import 'dart:io';
void main() {
File file = new File("/abc.txt");
Path path = new Path(file.path);
print(path.filename); // abc.txt
print(path.directoryPath); // /
}
You can get multiple ways to get filename, extension and directory path in dart and flutter
🧮 Tags
Recent posts
Puppeteer Login Test example How to convert Double to Integer or Integer to double in Dart| Flutter By Example Best ways to fix 504 gateway time out in nodejs Applications Fix for Error No configuration provided for scss Multiple ways to List containers in a Docker with examplesRelated posts