openWrite method

  1. @override
Future<FileWriteSession> openWrite(
  1. String path, {
  2. FilegateWriteMode mode = FilegateWriteMode.replace,
})
override

Implementation

@override
Future<FileWriteSession> openWrite(
  String path, {
  FilegateWriteMode mode = FilegateWriteMode.replace,
}) async {
  if (path.isEmpty) {
    throw ArgumentError.value(path, 'path', 'path must not be empty');
  }

  final sessionId = await methodChannel.invokeMethod<String>('startWrite', {
    'path': path,
    'mode': mode.name,
  });

  if (sessionId == null || sessionId.isEmpty) {
    throw PlatformException(
      code: FilegateErrorCode.missingWriteSessionId,
      message: 'Native writer did not return a session identifier.',
    );
  }

  return FileWriteSession(
    onAdd: (chunk) {
      return methodChannel.invokeMethod<void>('writeChunk', {
        'sessionId': sessionId,
        'bytes': chunk,
      });
    },
    onClose: () async {
      final entry = await methodChannel.invokeMapMethod<Object?, Object?>(
        'finishWrite',
        {'sessionId': sessionId},
      );
      return PickedEntry.fromMap(_castMap(entry));
    },
    onCancel: () => _cancelWrite(sessionId),
  );
}