checkValidIndex method

int checkValidIndex (
  1. int index,
  2. dynamic indexable,
  3. [String name,
  4. int length,
  5. String message]

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

Throws if index is not a valid index into indexable.

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

If name or message are provided, they are used as the parameter name and message text of the thrown error. If name is omitted, it defaults to "index".

If length is provided, it is used as the length of the indexable object, otherwise the length is found as indexable.length.

Returns index if it is a valid index.


static int checkValidIndex(int index, dynamic indexable,
    [String name, int length, String message]) {
  length ??= indexable.length;
  // Comparing with `0` as receiver produces better dart2js type inference.
  if (0 > index || index >= length) {
    name ??= "index";
    throw RangeError.index(index, indexable, name, message, length);
  return index;