requestAccess method
Asks for access, which must be declared under dartvel.fileStorage.
Answers when it is held. Throws DVFileAccessDenied when the person refuses or the platform has no such access, and StateError when the project never declared it -- asking for an undeclared permission is refused without a dialog on Android, and ends the app on iOS.
Implementation
Future<void> requestAccess(DVDeviceFileAccess access) async {
if (!_config.allows(access)) {
throw StateError(
'DV.Platform.fileStorage.requestAccess(${access.key}): add ${access.key} to '
'dartvel.fileStorage.access in pubspec.yaml so the build declares it.',
);
}
// A picked document is granted by being picked.
if (access == DVDeviceFileAccess.documents) return;
if (!DVNativeBridge.isRegistered('permissions.request')) {
throw DVFileAccessDenied(access, 'this target has no permissions binding to ask with.');
}
final bool? granted =
await DVNativeBridge.invoke<bool>('permissions.request', <String, Object?>{'permission': access.key});
if (granted != true) {
throw DVFileAccessDenied(
access,
access == DVDeviceFileAccess.allFiles
? 'all-files access was not switched on for this app in Settings.'
: 'the person refused ${access.key} access.',
);
}
}