Answers for "dart enum string"

1

flutter enum from string

EnumName myEnum = <EnumName>.values.firstWhere((e) => describeEnum(e) == str);
Posted by: Guest on September-16-2021
6

dart enum

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]}'); 
}
Posted by: Guest on August-06-2020
0

dart enum string

enum Enum {
	enum1, enum2, enum3,
}


/// This calss does the job of converting from/to String for enum.
/// 
/// The alternative is to use built_value enum which kinda generates it
/// automatically. 
class EnumFactory {
  static final Map<String, Enum> _stringMap = {
    'enum1': Enum.enum1,
    'enum2': Enum.enum2,
    'enum3': Enum.enum3,
  };

  static Enum fromString(String string) {
    return _stringMap[string];
  }

  static String itemToString(Enum item) {
    return _stringMap.keys
        .firstWhere((key) => _stringMap[key] == item, orElse: () => null);
  }
}
Posted by: Guest on July-26-2021
0

dart enum from string

Fruit f = Fruit.values.firstWhere((e) => e.toString() == 'Fruit.' + str);
Posted by: Guest on April-08-2021

Code answers related to "Dart"

Browse Popular Code Answers by Language