Check that a value 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 length is provided, it is used as the length of the indexable object, otherwise the length is found as indexable.length.
static void 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);
}
}
© 2012 the Dart project authors
Licensed under the Creative Commons Attribution-ShareAlike License v4.0.
https://api.dart.dev/stable/2.5.0/dart-core/RangeError/checkValidIndex.html