generic_enum_example 0.0.2 generic_enum_example: ^0.0.2 copied to clipboard
Demonstrates how to define and build generic enumeration classes using the library generic_enum.
Generic Enum Example #
Introduction #
GenericEnum is a base class for creating generic classes with a fixed set of static constant instances. These classes appear to the user like a Dart enum would. For example, generic enums can be used in switch statements, to initialize variables, or as default parameters in functions and constructors.
This library demonstrates how to define and built generic enumeration classes.
Starting Point #
This package provides a complete example on how to define and build
generic enumeration classes. As a starting point users could clone this repository, add
their own generic enum classes to the folder lib
and build the library. Build instructions
are provided below.
Boilerplate #
To use this library include generic_enum_annotation as dependency in your pubspec.yaml file. Include generic_enum_builder, source_gen, build_runner as dev_dependencies.
To create a generic enum class, say DpiResolution
, the following steps are required:
-
Extend
GenericEnum<T>
. To use json-serialization,T
should havefromJson
andtoJson
methods. -
Annotate the class with
@GenerateBuiltMap
and@GenerateToFromJson
. -
Define a private
const
constructor that calls the super constructor and passes on the value of typeT
. -
Define the static const instances of
DpiResolution
. You may capitalize instance names to mark them as constants. -
Define accessors for the private variable
_valueMap
and the function_toJson
.import 'package:built_collection/built_collection.dart'; import 'package:generic_enum/generic_enum.dart'; import 'package:generic_enum_annotation/generic_enum_annotation.dart'; // 0. Add a part statement pointing to the generated file. part 'dpi_resolution.g.dart'; // 1. Extend GenericEnum<T> @GenerateBuiltMap() // <----------- 2. Annotate class @GenerateToFromJson() // class DpiResolution extends GenericEnum<int> { // 3. Define a private const constructor that calls the super constructor // and passes on the value of type int. const DpiResolution._(int value) : super(value); // 4. Define static constant instances of type DpiResolution static const DpiResolution LOW = DpiResolution._(90); static const DpiResolution MEDIUM = DpiResolution._(300); static const DpiResolution HIGH = DpiResolution._(600); // 5. Give access to _valueMap and _toJson. static BuiltMap<int, DpiResolution> get valueMap => _valueMap; Map<String, dynamic> toJson() => _toJson(this); }
-
Configure the build targets (and amend the generate_for entry). In your local
build.yaml
file add the following targets:targets: $default: builders: # Configure the builder `pkg_name|builder_name` generic_enum_builder|map_builder: # Only run this builder on the specified input. enabled: true generate_for: - lib/*.dart # Configure the builder `pkg_name|builder_name` generic_enum_builder|json_builder: # Only run this builder on the specified input. enabled: true generate_for: - lib/*.dart
-
Build the project by running the command
$ pub run build_runner build --delete-conflicting-outputs
The generated file
dpi_resolution.g.dart
should have the following content:// GENERATED CODE - DO NOT MODIFY BY HAND part of 'dpi_resolution.dart'; // ************************************************************************** // JsonGenerator // ************************************************************************** Map<String, dynamic> _toJson(DpiResolution instance) => instance.toJson(); DpiResolution fromJson(Map<String, dynamic> json) { int value = GenericEnum.fromJson(json).value; return DpiResolution.valueMap[value]; } // ************************************************************************** // MapGenerator // ************************************************************************** final _valueMap = BuiltMap<int, DpiResolution>({ DpiResolution.LOW.value: DpiResolution.LOW, DpiResolution.MEDIUM.value: DpiResolution.MEDIUM, DpiResolution.HIGH.value: DpiResolution.HIGH });
Features and bugs #
Please file feature requests and bugs at the issue tracker.