乐闻世界logo
搜索文章和话题

How to Get the Filename in Dart?

2月7日 11:29

In Dart, you can use the path package to retrieve the filename. First, import the path package into your project:

dart
import 'package:path/path.dart' as path;

Then, use the path.basename() function to obtain the filename. This function takes a file path as input and returns the filename. For example:

dart
void main() { var filePath = '/path/to/the/file.txt'; var fileName = path.basename(filePath); print(fileName); // Output: file.txt }

To retrieve the filename without the extension, use path.basenameWithoutExtension():

dart
void main() { var filePath = '/path/to/the/file.txt'; var fileNameWithoutExtension = path.basenameWithoutExtension(filePath); print(fileNameWithoutExtension); // Output: file }
标签:Dart