Dart How to get the “value” of an enum

后端 未结 15 1841
野的像风
野的像风 2020-12-29 00:50

Before enums were available in Dart I wrote some cumbersome and hard to maintain code to simulate enums and now want to simplify it. I need to get the value of the enum as

相关标签:
15条回答
  • 2020-12-29 01:09

    Dart 2.7 comes with new feature called Extension methods. Now you can write your own methods for Enum as simple as that!

    enum Day { monday, tuesday }
    
    extension ParseToString on Day {
      String toShortString() {
        return this.toString().split('.').last;
      }
    }
    
    main() {
      Day monday = Day.monday;
      print(monday.toShortString()); //prints 'monday'
    }
    
    0 讨论(0)
  • 2020-12-29 01:11

    There is a more elegant solution:

    enum SomeStatus {
      element1,
      element2,
      element3,
      element4,
    }
    
    const Map<SomeStatus, String> SomeStatusName = {
      SomeStatus.element1: "Element 1",
      SomeStatus.element2: "Element 2",
      SomeStatus.element3: "Element 3",
      SomeStatus.element4: "Element 4",
    };
    
    print(SomeStatusName[SomeStatus.element2]) // prints: "Element 2"
    
    0 讨论(0)
  • 2020-12-29 01:13

    I had the same problem in one of my projects and existing solutions were not very clean and it didn't support advanced features like json serialization/deserialization.

    Flutter natively doesn't currently support enum with values, however, I managed to develop a helper package Vnum using class and reflectors implementation to overcome this issue.

    Address to the repository:

    https://github.com/AmirKamali/Flutter_Vnum

    To answer your problem using Vnum, you could implement your code as below:

    @VnumDefinition
    class Visibility extends Vnum<String> {
      static const VISIBLE = const Visibility.define("VISIBLE");
      static const COLLAPSED = const Visibility.define("COLLAPSED");
      static const HIDDEN = const Visibility.define("HIDDEN");
    
      const Visibility.define(String fromValue) : super.define(fromValue);
      factory Visibility(String value) => Vnum.fromValue(value,Visibility);
    }
    

    You can use it like :

    var visibility = Visibility('COLLAPSED');
    print(visibility.value);
    

    There's more documentation in the github repo, hope it helps you out.

    0 讨论(0)
  • 2020-12-29 01:18

    This is your enum:

    enum Day {
      monday,
      tuesday,
    }
    

    Create an extension (may need to import 'package:flutter/foundation.dart';)

    extension DayEx on Day {
      String get inString => describeEnum(this);
    }
    

    Usage:

    void main() {
      Day monday = Day.monday;
      String inString = monday.inString; // 'monday'
    }
    
    0 讨论(0)
  • 2020-12-29 01:20

    Sadly, you are correct that the toString method returns "day.MONDAY", and not the more useful "MONDAY". You can get the rest of the string as:

    day theDay = day.MONDAY;      
    print(theDay.toString().substring(theDay.toString().indexOf('.') + 1));
    

    Hardly convenient, admittedly.

    If you want to iterate all the values, you can do it using day.values:

    for (day theDay in day.values) {
      print(theDay);
    }
    
    0 讨论(0)
  • 2020-12-29 01:24
    enum day {MONDAY, TUESDAY}
    print( 'Today is ${describeEnum(day.MONDAY)}' );
    

    console output: Today is MONDAY

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