尝试获取文件树时出现此错误
我在我的项目中使用这些插件:
路径提供者:^0.5.0+1
简单权限:^0.1.9
import 'package:path_provider/path_provider.dart';
import 'package:simple_permissions/simple_permissions.dart';
...
static Future<String> get localPath async {
final directory = await getApplicationDocumentsDirectory();
return directory.path;
}
final List<FileSystemEntity> files = List<FileSystemEntity>();
Directory parent;
@override
Widget build(BuildContext context) {
if (parent == null) {
SimplePermissions
.requestPermission(Permission.WriteExternalStorage)
.then((value) {
if (value == PermissionStatus.authorized) {
localPath.then((String value) {
Directory dir = Directory(value);
while (dir.path != dir.parent.path) {
dir = dir.parent;
}
parent = dir;
setState(() {
files.addAll(dir.listSync());
sortFiles();
});
});
} else {
SimplePermissions.openSettings();
}
});
}
return Scaffold(...)
}
它在 Nexus 5 上运行良好,但在 Pixel 3 上出现错误
[错误:flutter/lib/ui/ui_dart_state.cc(148)] 未处理的异常:FileSystemException:目录列表失败,路径 = '/'(操作系统错误:权限被拒绝,errno = 13)
产生的错误dir.listSync()
有谁知道如何解决这个问题?