EnumByName<T extends Enum> extension

为枚举集合 Iterable<T> 提供根据索引查找值的功能。

该扩展适用于实现了 Enum 的类型 T,用于通过索引快速查找枚举值。

使用场景

  • 当需要根据索引动态获取枚举值时,可使用此方法代替手动遍历。

注意事项

  • 如果指定的索引在枚举集合中不存在,会抛出 ArgumentError

示例

enum Colors { red, green, blue }

void main() {
  final colors = Colors.values;
  final green = colors.byIndex(1); // 获取 Colors.green
  print(green); // 输出:Colors.green
}
on

Methods

byIndex(int index) → T

Available on Iterable<T>, provided by the EnumByName extension

根据给定的索引 index 查找并返回对应的枚举值。