Add methods or values to enum in dart

前端 未结 8 956
刺人心
刺人心 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:00

    I did this (inspired form the accepted answer by @vovahost)

    enum CodeVerifyFlow{
      SignUp, Recovery, Settings
    }
    
    extension CatExtension on CodeVerifyFlow {
      String get name {
        return ["sign_up", "recovery", "settings"][this.index];
      }
    }
    
    // use it like
    CodeVerifyFlow.SignUp.name
    

    thank me later!

    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-12-14 06:06

    It may not be "Effective Dart" , I add a static method inside a Helper class ( there is no companion object in Dart) .

    In your color.dart file

    enum Color {
      red,
      green,
      blue
    }
    
    class ColorHelper{
    
      static String getValue(Color color){
        switch(color){
          case Color.red: 
            return "Red";
          case Color.green: 
            return "Green";
          case Color.blue: 
            return "Blue";  
          default:
            return "";
        }
      }
    
    }
    

    Since the method is in the same file as the enum, one import is enough

    import 'package:.../color.dart';
    
    ...
    String colorValue = ColorHelper.getValue(Color.red);
    
    0 讨论(0)
  • 2020-12-14 06:10

    As an improvement on the other suggestions of using Extensions, you can define your assigned values in a list of map, and the extension will be concise.

    enum Numbers {
      one,
      two,
      three,
    }
    
    // Numbers.one.value == 1
    

    example with list

    extension NumbersExtensionList on Numbers {
      static const values = [1, 2, 3];
      int get value => values[this.index];
    }
    

    example with map

    extension NumbersExtensionMap on Numbers {
      static const valueMap = const {
        Numbers.one: 1,
        Numbers.two: 2,
        Numbers.three: 2,
      };
      int get value => valueMap[this];
    }
    

    Note: This approach has the limitation that you can not define a static factory method on the Enum, e.g. Numbers.create(1) (as of Dart 2.9). You can define this method on the NumbersExtension, but it would need to be called like NumbersExtension.create(1)

    0 讨论(0)
  • 2020-12-14 06:11

    For String returns :

    enum Routes{
      SPLASH_SCREEN,
      HOME,
      // TODO Add according to your context
    }
    
    String namedRoute(Routes route){
      final runtimeType = '${route.runtimeTypes.toString()}.';
      final output = route.toString();
      return output.replaceAll(runtimeType, "");
    }
    
    0 讨论(0)
  • 2020-12-14 06:12

    Starting with Dart 2.6 you can define extensions on classes (Enums included).

    enum Cat {
      black,
      white
    }
    
    extension CatExtension on Cat {
    
      String get name {
        switch (this) {
          case Cat.black:
            return 'Mr Black Cat';
          case Cat.white:
            return 'Ms White Cat';
          default:
            return null;
        }
      }
    
      void talk() {
        print('meow');
      }
    }
    

    Example:

    Cat cat = Cat.black;
    String catName = cat.name;
    cat.talk();
    

    Here's one more live example (uses a constant map instead of a switch): https://dartpad.dartlang.org/c4001d907d6a420cafb2bc2c2507f72c

    0 讨论(0)
提交回复
热议问题