onReceiveCreateNewInstancePair method

Object? onReceiveCreateNewInstancePair(
  1. String channelName,
  2. PairedInstance pairedInstance,
  3. List<Object?> arguments, {
  4. required bool owner,
})

Create and store a new instance pair for a type channel.

Throw an assertion error if pairedInstance has already been added. Otherwise, it returns the paired object.

Implementation

Object? onReceiveCreateNewInstancePair(
  String channelName,
  PairedInstance pairedInstance,
  List<Object?> arguments, {
  required bool owner,
}) {
  assert(
    getPairedObject(pairedInstance) == null,
    'An object with `PairedInstance` has already been created.',
  );

  final TypeChannelHandler? handler = getChannelHandler(channelName);
  if (handler == null) {
    throw ArgumentError(
      'A `TypeChannelHandler` must be set for channel of: $channelName.',
    );
  }

  final Object instance = handler.createInstance(
    this,
    converter.convertPairedInstances(instanceManager, arguments)!
        as List<Object?>,
  );

  assert(!isPaired(instance), '`$instance` has already been paired.');

  _addInstancePair(
    instance: instance,
    instanceId: pairedInstance.instanceId,
    owner: owner,
  );
  return instance;
}