addCustomOp<T extends NativeType> method

void addCustomOp<T extends NativeType>({
  1. required String name,
  2. required Pointer<T> registration,
  3. int minVersion = 1,
  4. int maxVersion = 1,
})

Registers a custom op with these interpreter options.

registration must point to a valid TfLiteRegistration returned by your native custom-op library. The registration's contents must remain valid for the lifetime of every interpreter created from these options.

The op name must match the custom op name embedded in the .tflite model. This method keeps the native op-name string alive until delete.

Call this before creating the interpreter.

Example:

final options = InterpreterOptions();
options.addCustomOp(
  name: 'MyCustomOpName',
  registration: registrationPointer,
);
final interpreter = await Interpreter.fromAsset('model.tflite', options: options);

Implementation

void addCustomOp<T extends NativeType>({
  required String name,
  required Pointer<T> registration,
  int minVersion = 1,
  int maxVersion = 1,
}) {
  checkState(!_deleted, message: 'InterpreterOptions already deleted.');
  checkArgument(
    name.isNotEmpty,
    message: 'Custom op name must not be empty.',
  );
  checkArgument(
    minVersion >= 1,
    message: 'minVersion must be greater than or equal to 1.',
  );
  checkArgument(
    maxVersion >= minVersion,
    message: 'maxVersion must be greater than or equal to minVersion.',
  );

  final opName = name.toNativeUtf8().cast<Char>();
  _customOpNames.add(opName);
  _customOps.add(
    _CustomOpConfiguration(
      name: name,
      registration: registration.cast<TfLiteRegistration>(),
      minVersion: minVersion,
      maxVersion: maxVersion,
    ),
  );
  tfliteBinding.TfLiteInterpreterOptionsAddCustomOp(
    _options,
    opName,
    registration.cast<TfLiteRegistration>(),
    minVersion,
    maxVersion,
  );
}