check static method

  1. @Since("2.19")
int check(
  1. int index,
  2. int length, {
  3. Object? indexable,
  4. String? name,
  5. String? message,
})

Check that index is a valid index into an indexable object.

Throws if index is not a valid index.

An indexable object is one that has a length and an index-operator [] that accepts an index if 0 <= index < length.

The length is the length of the indexable object.

The indexable, if provided, is the indexable object.

The name is the parameter name of the index value. Defaults to "index", and can be set to null to omit a name from the error string, if the invalid index was not a parameter.

The message, if provided, is included in the error string.

Returns index if it is a valid index.

Implementation

@Since("2.19")
static int check(int index, int length,
    {Object? indexable, String? name, String? message}) {
  // Comparing with `0` as receiver produces better dart2js type inference.
  if (0 > index || index >= length) {
    name ??= "index";
    throw IndexError.withLength(index, length,
        indexable: indexable, name: name, message: message);
  }
  return index;
}