Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dart. Enum with arguments

Is it possible to create enum with arguments. For example in Kotlin I can do something like this:

  enum class TestType(val testText: String, val number: Int) {
        STATIC("", 0),
        DYNAMIC("", 1)
    }

And each enum item contains testText and number. Is it possible to implement this in the Dart programming language. Please help me.

like image 545
testivanivan Avatar asked Sep 03 '26 18:09

testivanivan


1 Answers

Dart 2.17 enhanced functionality provide way to add arguments in enums,

Example:

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

  const TestType(this.text, this.value);
  final String text;
  final int value;
}

To use it:

void main() {
  const testType = TestType.STATIC;
  print(testType.value); // 0
}

Note: To enable this functionality, need to modify pubspec dart version to 2.17 like

environment:
  sdk: ">=2.17.0 <3.0.0"
like image 150
Jitesh Mohite Avatar answered Sep 06 '26 09:09

Jitesh Mohite



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!