maybeFromValue<T extends Object> static method

Script? maybeFromValue<T extends Object>(
  1. T value, {
  2. T? where(
    1. Script script
    )?,
  3. Iterable<Script> scripts = list,
})

Returns a Script instance that corresponds to the given value.

The value parameter is required and represents the value to match against. The optional where parameter is a function that can be used to specify a custom comparison logic for finding the matching Script instance. The optional scripts parameter can be used to specify a list of Script objects to search through. This method returns the Script instance that corresponds to the given value, or null if no such instance exists.

Implementation

static Script? maybeFromValue<T extends Object>(
  T value, {
  T? Function(Script script)? where,
  Iterable<Script> scripts = list,
}) {
  // ignore: avoid-collection-mutating-methods, not mutating anything.
  scripts.assertNotEmpty();

  for (final script in scripts) {
    final expectedValue = where?.call(script) ?? script.code;
    if (expectedValue == value) return script;
  }

  return null;
}