我已经将文件路径保存在变量中,我想通过使用路径包https://pub.dev/packages/path来获取文件类型扩展名到目前为止,我设法通过像这样拆分字符串来做到这一点
final path = "/some/path/to/file/file.dart";
print(path.split(".").last); //prints dart
Run Code Online (Sandbox Code Playgroud)
有没有办法用路径包来实现这一点?
cre*_*not 20
您可以使用该extension功能在path包获取某个文件路径的扩展:
import 'package:path/path.dart' as p;
final path = '/some/path/to/file/file.dart';
final extension = p.extension(path); // '.dart'
Run Code Online (Sandbox Code Playgroud)
如果您的文件有多个扩展名,例如file.dart.js,您可以指定可选level参数:
final extension = p.extension('file.dart.js', 2); // '.dart.js'
Run Code Online (Sandbox Code Playgroud)
Yas*_*ash 10
不需要任何扩展。您可以尝试下面的代码片段。
String getFileExtension(String fileName) {
try {
return "." + fileName.split('.').last;
} catch(e){
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7014 次 |
| 最近记录: |