withScopedAccess<T> static method

Future<T> withScopedAccess<T>(
  1. String path,
  2. Uint8List? bookmark,
  3. Future<T> body(
    1. String path
    )
)

Run body with scoped access to a file. On macOS / iOS, when bookmark is non-null, the plugin resolves the bookmark into a security-scoped URL, starts access, hands the resolved path to body, and stops access in finally. On all other platforms (and when bookmark is null) body is invoked with path directly.

The plugin maintains a per-path begin counter so nested calls are safe. Access is always released in finally, including on errors.

Implementation

static Future<T> withScopedAccess<T>(
  String path,
  Uint8List? bookmark,
  Future<T> Function(String path) body,
) async {
  if (bookmark == null || !(Platform.isMacOS || Platform.isIOS)) {
    return body(path);
  }
  final result = await _channel.invokeMapMethod<String, Object?>(
    'beginScopedAccess',
    {'bookmark': bookmark},
  );
  final resolvedPath = result?['path'] as String? ?? path;
  try {
    return await body(resolvedPath);
  } finally {
    await _channel.invokeMethod<void>('endScopedAccess', {
      'path': resolvedPath,
    });
  }
}