checkValidRange 静态方法

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

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

如果范围对于具有给定 length 的可索引对象不有效,则抛出异常。如果 0 <= [start] <= [end] <= [length],则范围对于具有给定 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;
}