getDirectoryPermission method
Request the user for access to Directory Permission
, if access hasn't already
been grant access before.
Returns bool
.
Implementation
Future<bool?> getDirectoryPermission(
{bool grantWritePermission = true, bool isDynamic = false}) async {
try {
/// Check if user has already Permission Granted
var isGranted = await isPersistedPermissionDirectoryFor(_uriString);
if (isGranted != null && isGranted) return true;
const kOpenDocumentTree = 'openDocumentTree';
const kGrantWritePermission = 'grantWritePermission';
const kInitialUri = 'initialUri';
/// Initial location of native file explorer
/// when user is prompted to chose the directory
String initialUri = makeUriString(path: _directory);
final args = <String, dynamic>{
kGrantWritePermission: grantWritePermission,
kInitialUri: initialUri
};
/// Get the URI of user selected Directory path
final selectedDirectoryUri = await kDocumentFileChannel
.invokeMethod<String?>(kOpenDocumentTree, args);
if (isDynamic) {
_uriString = selectedDirectoryUri;
_directory = makeDirectoryPath(_uriString!);
}
if (!isDynamic && selectedDirectoryUri != _uriString) {
releasePersistableUriPermission(selectedDirectoryUri);
return false;
}
return true;
} catch (e) {
return null;
}
}