Add methods or values to enum in dart

前端 未结 8 977
刺人心
刺人心 2020-12-14 05:21

In java when you are defining an enum you can do something similar to the following. Is this possible in Dart?

enum blah {
  one(1), two(2);
  final num valu         


        
8条回答
  •  情书的邮戳
    2020-12-14 06:06

    Nope. In Dart, enums can only contain the enumerated items:

    enum Color {
      red,
      green,
      blue
    }
    

    However, each item in the enum automatically has an index number associated with it:

    print(Color.red.index);    // 0
    print(Color.green.index);  // 1
    

    You can get the values by their index numbers:

    print(Color.values[0] == Color.red);  // True
    

    See: https://www.dartlang.org/guides/language/language-tour#enums

提交回复
热议问题