Search code examples
fluttergoogle-pixel

FileSystemException: OS Error: Permission denied in Google Pixel 3


I'm getting this error when trying to get file tree

I'm using these plugins in my project:

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(...)
}

It works totally fine on Nexus 5, but on Pixel 3 I get an error

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

Error produced by dir.listSync()

Does anybody know how to solve this?


Solution

  • dir.listSync() can return exception, so you have to check every parent directory if it available for getting files inside it