FileSystemException: OS Error: Permission denied in Google Pixel 3

FileSystemException: OS Error: Permission denied in Google Pixel 3

我在尝试获取文件树时遇到此错误

我在我的项目中使用这些插件:

path_provider: ^0.5.0+1

simple_permissions: ^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 上出现错误

[ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: FileSystemException: Directory listing failed, path = '/' (OS Error: Permission denied, errno = 13)

dir.listSync()

产生的错误

有人知道怎么解决吗?

dir.listSync() 可以 return 异常,因此您必须检查每个父目录是否可以获取其中的文件