I found I can't get the name of a file in a simple way :(
Dart code:
File file = new File("/dev/dart/work/hello/app.dart");
How to get the file name app.dart
?
I don't find an API for this, so what I do is:
var path = file.path;
var filename = path.split("/").last;
Is there any simpler solution?
To read File as a String in Dart, you can use File. readAsString() method or File. readAsStringSync(). File.
How to list the contents of a directory in Dart. final dir = Directory('path/to/directory'); final List<FileSystemEntity> entities = await dir. list(). toList();
You can use the path package :
import 'dart:io';
import 'package:path/path.dart';
main() {
File file = new File("/dev/dart/work/hello/app.dart");
String filename = basename(file.path);
}
If you don't want to use the path pub package, you can use the Uri class from dart:io that returns the filename and its extension:
String fileName = File(localFilePath).uri.pathSegments.last;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With