Dart json_serializable enum values

Here is an example to serialize string value to enum using the json_serializable library in Dart. All you have to do is to add the annotation @JsonValue with the actual string before each enum type.

import 'package:json_annotation/json_annotation.dart';

enum PokemonType {
  @JsonValue("Bug")
  bug,

  @JsonValue("Electric")
  electric,

  @JsonValue("Fairy")
  fairy,

  @JsonValue("Fighting")
  fighting,

  @JsonValue("Fire")
  fire,

  @JsonValue("Flying")
  flying
}

Search within Codexpedia

Custom Search

Search the entire web

Custom Search