[Flutter] Easy use enum in flutter version 3

enum HogeType {
  hoge('ほげ', 'hoge'),
  fuga('ふが', 'fuga'),
  piyo('ぴよ', 'piyo'),
  foo('ふー', 'foo'),
  bar('ばー', 'bar'),
  none('', '');

  const HogeType(this.name, this.type);
  final String name;
  final String type;

  static HogeType fromType(String from) {
    return HogeType.values.firstWhere(
      (value) {
        return value.type == from;
      },
      orElse: () {
        return HogeType.none;
      },
    );
  }
}

void hg() {
  print(HogeType.fromType('hoge'));
  //HogeType.hoge
  print(HogeType.fromType('aa'));
  //HogeType.none
}

Comments !

Links

Social