LoginSignup
4
0

More than 5 years have passed since last update.

Dartでenumの列挙子名をStringで取得する方法

Posted at

列挙子名を取得する関数などは見つかりませんでした。

toStringを使うと <型名>.<列挙子名> が取得できるので、<型名>.を置換して空文字にすることで列挙子名を得ることができます。

enum Status {
  ok,ng
}

void main() {
  Status status = Status.ok;
  print(status.toString()); // Status.ok
  print(status.toString().replaceFirst(status.toString().split('.')[0] + "." , "" )); // ok
}
4
0
2

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
4
0