close method

  1. @override
Future<void> close()
override

Implementation

@override
Future<void> close() async {
  _isClosed = true;

  // Cancel event subscription first to stop receiving events
  await _eventSubscription?.cancel();
  _eventSubscription = null;

  // Try to stop generation if possible (ignore errors on unsupported platforms)
  try {
    await _platformService.stopGeneration();
  } on PlatformException catch (e) {
    // Ignore "not supported" errors, but rethrow others
    if (e.code != 'stop_not_supported') {
      if (kDebugMode) {
        print('Warning: Failed to stop generation: ${e.message}');
      }
    }
  } catch (e) {
    // Ignore other errors during cleanup
    if (kDebugMode) {
      print('Warning: Unexpected error during stop generation: $e');
    }
  }

  // Close controller after stopping subscription
  _asyncResponseController?.close();

  onClose();
  await _platformService.closeSession();
}