switchModel method

Future<void> switchModel(
  1. String newModelPath,
  2. YOLOTask newTask
)

Switches the model on the associated YoloView.

This method allows switching to a different model without recreating the view. The view must be initialized (have a viewId) before calling this method.

@param newModelPath The path to the new model @param newTask The task type for the new model @throws StateError if the view is not initialized @throws ModelLoadingException if the model switch fails

Implementation

Future<void> switchModel(String newModelPath, YOLOTask newTask) async {
  if (_viewId == null) {
    throw StateError('Cannot switch model: view not initialized');
  }

  try {
    final Map<String, dynamic> arguments = {
      'viewId': _viewId,
      'modelPath': newModelPath,
      'task': newTask.name,
      'useGpu': useGpu,
    };

    // Only include instanceId for multi-instance mode
    if (_instanceId != 'default') {
      arguments['instanceId'] = _instanceId;
    }

    await _channel.invokeMethod('setModel', arguments);
  } on PlatformException catch (e) {
    if (e.code == 'MODEL_NOT_FOUND') {
      throw ModelLoadingException('Model file not found: $newModelPath');
    } else if (e.code == 'INVALID_MODEL') {
      throw ModelLoadingException('Invalid model format: $newModelPath');
    } else if (e.code == 'UNSUPPORTED_TASK') {
      throw ModelLoadingException(
        'Unsupported task type: ${newTask.name} for model: $newModelPath',
      );
    } else {
      throw ModelLoadingException('Failed to switch model: ${e.message}');
    }
  } catch (e) {
    throw ModelLoadingException('Unknown error switching model: $e');
  }
}