镖。带参数的枚举

tes*_*van 6 enums dart flutter

是否可以创建带有参数的枚举。例如,在 Kotlin 中我可以这样做:

  enum class TestType(val testText: String, val number: Int) {
        STATIC("", 0),
        DYNAMIC("", 1)
    }
Run Code Online (Sandbox Code Playgroud)

每个枚举项包含 testText 和 number。是否可以用 Dart 编程语言来实现这一点。请帮我。

jit*_*555 7

Dart 2.17增强的功能提供了在枚举中添加参数的方法,

例子:

enum TestType {
   STATIC("", 0),
   DYNAMIC("", 1);

  const TestType(this.text, this.value);
  final String text;
  final int value;
}
Run Code Online (Sandbox Code Playgroud)

使用方法:

void main() {
  const testType = TestType.STATIC;
  print(testType.value); // 0
}
Run Code Online (Sandbox Code Playgroud)

注意:要启用此功能,需要将pubspec dart 版本修改为 2.17,例如

environment:
  sdk: ">=2.17.0 <3.0.0"
Run Code Online (Sandbox Code Playgroud)