Does Dart support enumerations?

EnumsDart

Enums Problem Overview


Does Dart support enumerations? For instance:

enum myFruitEnum { Apple, Banana }

A cursory search of the docs suggests no.

Enums Solutions


Solution 1 - Enums

Beginning 1.8, you can use enums like this:

enum Fruit {
  apple, banana
}

main() {
  var a = Fruit.apple;
  switch (a) {
    case Fruit.apple:
      print('it is an apple');
      break;
  }

  // get all the values of the enums
  for (List<Fruit> value in Fruit.values) {
    print(value);
  }

  // get the second value
  print(Fruit.values[1]);
}

The old approach before 1.8:

class Fruit {
  static const APPLE = const Fruit._(0);
  static const BANANA = const Fruit._(1);

  static get values => [APPLE, BANANA];

  final int value;

  const Fruit._(this.value);
}

Those static constants within the class are compile time constants, and this class can now be used in, for example, switch statements:

var a = Fruit.APPLE;
switch (a) {
  case Fruit.APPLE:
    print('Yes!');
    break;
}

Solution 2 - Enums

With r41815 Dart got native Enum support see http://dartbug.com/21416 and can be used like

enum Status {
  none,
  running,
  stopped,
  paused
}

void main() {
  print(Status.values);
  Status.values.forEach((v) => print('value: $v, index: ${v.index}'));
  print('running: ${Status.running}, ${Status.running.index}');
  print('running index: ${Status.values[1]}');
}

> [Status.none, Status.running, Status.stopped, Status.paused]
> value: Status.none, index: 0
> value: Status.running, index: 1
> value: Status.stopped, index: 2
> value: Status.paused, index: 3
> running: Status.running, 1
> running index: Status.running

A limitation is that it is not possibly to set custom values for an enum item, they are automatically numbered.

More details at in this draft https://www.dartlang.org/docs/spec/EnumsTC52draft.pdf

Solution 3 - Enums

Enumeration should be available in the future. But until Enum has landed you can do something like :

class Fruit {
  static final APPLE = new Fruit._();
  static final BANANA = new Fruit._();

  static get values => [APPLE, BANANA];

  Fruit._();
}

Solution 4 - Enums

This and this may be the answers on your question:

... for the technology preview it was decided to leave it out and just 
use static final fields for now. It may be added later.

You can still do something like this:

interface ConnectionState { }
class Connected implements ConnectionState { }
class Connecting implements ConnectionState { }
class Disconnected implements ConnectionState { }

//later
ConnectionState connectionState;
if (connectionState is Connecting) { ... }

wich is in my opinion more clear for use. It's a bit more difficult for programming the application structure, but in some cases, it's better and clear.

Solution 5 - Enums

how about this approach:

class FruitEnums {
  static const String Apple = "Apple";
  static const String Banana = "Banana";
}

class EnumUsageExample {

  void DoSomething(){

    var fruit = FruitEnums.Apple;
    String message;
    switch(fruit){
      case(FruitEnums.Apple):
        message = "Now slicing $fruit.";
        break;
      default:
        message = "Now slicing $fruit via default case.";
        break;
    }
  }
}

Solution 6 - Enums

Yes! Its actually very useful to do Enums in Dart:

  enum fruits{
    BANANA, APPLE, ORANGE
  }

Solution 7 - Enums

Just use Types Class File.

Dart-Types

easy, fast, more powerful and more helpful.

little problem, it is this class limited to five Different Choice and plus one for acts as null.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBraveNewMathView Question on Stackoverflow
Solution 1 - EnumsKai SellgrenView Answer on Stackoverflow
Solution 2 - EnumsGünter ZöchbauerView Answer on Stackoverflow
Solution 3 - EnumsAlexandre ArdhuinView Answer on Stackoverflow
Solution 4 - Enumsuser35443View Answer on Stackoverflow
Solution 5 - EnumsBraveNewMathView Answer on Stackoverflow
Solution 6 - EnumsBeloin SenaView Answer on Stackoverflow
Solution 7 - EnumsNabilJaranView Answer on Stackoverflow