从枚举中获取JsonKey名称

huangapple go评论62阅读模式
英文:

Get JsonKey name back from enum

问题

如何从 Bear.blackBear 中获取 black_bear

英文:

I have a simple enum in Flutter, say

enum Bear {
    panda,
    @JsonValue("black_bear") blackBear,
    @JsonValue("brown_bear") brownBear,
}

How do I retrieve black_bear from Bear.blackBear?

答案1

得分: 1

你可以使用增强枚举,

enum Animal {
  panda('panda'),
  blackBear("black_bear"),
  brownBear("brown_bear");

  const Animal(this.key);
  final String key;
}

你可以使用 Animal.blackBear.key; 来获取 black_bear

英文:

You can use enhanced enum,

enum Animal {
  panda('panda'),
  blackBear("black_bear"),
  brownBear("brown_bear");

  const Animal(this.key);
  final String key;
}

And you can use Animal.blackBear.key; to get black_bear .

huangapple
  • 本文由 发表于 2023年5月21日 02:36:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76296816.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定