checkValidRange 静态方法

int checkValidRange(
  1. int start,
  2. int? end,
  3. int length,
  4. [String? startName,
  5. String? endName,
  6. String? message]
)

检查一个范围是否代表一个可索引对象的切片。

如果该范围对于给定 length 的可索引对象无效,则抛出异常。一个范围对于给定 length 的可索引对象有效

如果 0 <= [start] <= [end] <= [length]。当 endnull 时,视为等效于 length

startNameendName 的默认值分别为 "start""end"

返回实际 end 值,如果 endnull,则为 length,否则为 end

实现

static int checkValidRange(int start, int? end, int length,
    [String? startName, String? endName, String? message]) {
  // Comparing with `0` as receiver produces better dart2js type inference.
  // Ditto `start > end` below.
  if (0 > start || start > length) {
    startName ??= "start";
    throw RangeError.range(start, 0, length, startName, message);
  }
  if (end != null) {
    if (start > end || end > length) {
      endName ??= "end";
      throw RangeError.range(end, start, length, endName, message);
    }
    return end;
  }
  return length;
}