LoginSignup
1
0

More than 3 years have passed since last update.

DartでEnumの値を文字列で取得する

Posted at

Flutterでアプリを作っていて、Enumの値をAPIに文字列として送りたいときに、こんな感じで関数用意しておくと便利という備忘録。

String enumValueToString(Object o) => o.toString().split('.').last;

T enumValueFromString<T>(String key, List<T> values) =>
    values.firstWhere((v) => key == enumValueToString(v), orElse: () => null);
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0