rangeTo method

List<num> rangeTo(
  1. num other
)

Generates a list of numbers from the current number to other, inclusive.

If other is greater than the current number, the list is generated in ascending order. If other is less than the current number, the list is generated in descending order. The method accounts for both integer and decimal numbers by rounding the generated range's length to the nearest integer.

Parameters:

  • other: The end point of the range.

Returns: A list of numbers starting from the current number to other, inclusive.

Example:

print(1.rangeTo(5)); // Outputs: [1, 2, 3, 4, 5]
print(5.rangeTo(1)); // Outputs: [5, 4, 3, 2, 1]

Implementation

List<num> rangeTo(num other) => this <= other
    ? List.generate((other - this + 1).toInt(), (index) => this + index)
    : List.generate((this - other + 1).toInt(), (index) => this - index).reversed.toList();