Simple, extensible and powerful enumeration implementation
You must include EnumPlus in your Enum. If you are using enhanced enum. You should return your own value by overriding the toValue method.
import 'package:enum_plus/enum_plus.dart';
enum Animal with EnumPlus {
DOG(0),
CAT(1),
HONEY_BEE(2);
const Animal(this.value);
final int value;
@override
dynamic toValue() => value;
}
- Enum Methods
- Enum List Methods
- hasName()
- hasFriendlyName()
- getNames()
- getName()
- getNamesExcept()
- getFriendlyNames()
- getFriendlyNamesExcept()
- fromNames()
- fromFriendlyNames()
- fromName()
- fromFriendlyName()
- toListExcept()
- getRandom()
- getRandomName()
- getRandomFriendlyName()
- hasValue()
- getValues()
- getValue()
- getValuesExcept()
- fromValue()
- fromValues()
- coerce()
- getRandomValue()
Checks if this instance is equal to the given enum instance or value.
print(Animal.DOG.equal(Animal.DOG)); // true
print(Animal.DOG.equal(0)); // true
print(Animal.DOG.equal(Animal.CAT)); // false
print(Animal.DOG.equal(1)); // false
Checks if this instance is not equal to the given enum instance or value.
print(Animal.DOG.notEqual(Animal.CAT)); // true
print(Animal.DOG.notEqual(1)); // true
print(Animal.DOG.notEqual(Animal.DOG)); // false
print(Animal.DOG.notEqual(0)); // false
Checks if a matching enum instance or value is in the given values.
print(Animal.DOG.inside([Animal.DOG, Animal.CAT])); // true
print(Animal.DOG.inside([0, 1])); // true
print(Animal.DOG.inside([Animal.HONEY_BEE, Animal.CAT])); // false
print(Animal.DOG.inside([1, 2])); // false
Checks if a matching enum instance or value is not in the given values.
print(Animal.DOG.outside([Animal.HONEY_BEE, Animal.CAT])); // true
print(Animal.DOG.outside([1, 2])); // true
print(Animal.DOG.outside([Animal.DOG, Animal.CAT])); // false
print(Animal.DOG.outside([0, 1])); // false
Transform the name into a friendly, formatted version.
print(Animal.HONEY_BEE.getFriendlyName()); // Honey Bee
Check that the enum contains a specific name.
print(Animal.values.hasName('DOG')); // true
print(Animal.values.hasName('FISH')); // false
Check that the enum contains a specific friendly name.
print(Animal.values.hasFriendlyName('Honey Bee')); // true
print(Animal.values.hasFriendlyName('Dog')); // true
print(Animal.values.hasFriendlyName('Fish')); // false
Get all or a custom set of the enum names.
print(Animal.values.getNames()); // [DOG, CAT, HONEY_BEE]
print(Animal.values.getNames(values: [0, Animal.CAT])); // [DOG, CAT]
Get the name for a single enum value.
print(Animal.values.getName(1)); // CAT
print(Animal.values.getName(100)); //Bad state: No element
Return names of all the enums except the given values.
print(Animal.values.getNamesExcept([0, Animal.HONEY_BEE])); // [CAT]
Get all or a custom set of the enum friendly names.
print(Animal.values.getFriendlyNames()); // [Dog, Cat, Honey Bee]
print(Animal.values.getFriendlyNames(values: [0, Animal.HONEY_BEE])); // [Dog, Honey Bee]
Return friendly names of all the enums except the given values.
print(Animal.values.getFriendlyNamesExcept([Animal.DOG, 1])); // [Honey Bee]
Get enums from names.
print(Animal.values.fromNames(['DOG', 'CAT'])); // [Animal.DOG, Animal.CAT]
print(Animal.values.fromNames(['CAT', 'FISH'])); // Bad state: No element
Get enums from friendly names.
print(Animal.values.fromFriendlyNames(['Honey Bee', 'Cat'])); // [Animal.HONEY_BEE, Animal.CAT]
print(Animal.values.fromFriendlyNames(['Honey Bee', 'Fish'])); // Bad state: No element
Make an enum instance from a given key.
print(Animal.values.fromName('DOG')); // Animal.DOG
print(Animal.values.fromName('FISH')); // Bad state: No element
Make an enum instance from a given friendly name.
print(Animal.values.fromFriendlyName('Honey Bee')); // Animal.HONEY_BEE
print(Animal.values.fromFriendlyName('Fish')); // Bad state: No element
Return instances of all the enums except the given values.
print(Animal.values.toListExcept([Animal.DOG, 1])); // [Animal.HONEY_BEE]
Get a random instance of the enum.
print(Animal.values.getRandom()); // Animal.HONEY_BEE
Get a random name of the enum.
print(Animal.values.getRandomName()); // DOG
Get a random friendly name of the enum.
print(Animal.values.getRandomFriendlyName()); // Cat
Check that the enum contains a specific value.
print(Animal.values.hasValue(1)); // true
print(Animal.values.hasValue(Animal.CAT)); // true
print(Animal.values.hasValue(100)); // false
Get all or a custom set of the enum values.
print(Animal.values.getValues()); // [0, 1, 2]
print(Animal.values.getValues(names: ['DOG'])); // [0]
Get the value for a single enum key.
print(Animal.values.getValue('DOG')); // 0
print(Animal.values.getValue('FISH')); // Bad state: No element
Return values of all the enums except the given values.
print(Animal.values.getValuesExcept([Animal.DOG, 1])); // [2]
Make a new instance from an enum value.
print(Animal.values.fromValue(1)); // Animal.CAT
print(Animal.values.fromValue(Animal.CAT)); // Animal.CAT
print(Animal.values.fromValue(100)); // Bad state: No element
Return instances from enum values.
print(Animal.values.fromValues([0, Animal.CAT])); // [Animal.DOG, Animal.CAT]
Attempt to instantiate a new Enum using the given key or value.
print(Animal.values.coerce(1)); // Animal.CAT
print(Animal.values.coerce('CAT')); // Animal.CAT
print(Animal.values.coerce(100)); // null
print(Animal.values.coerce('FISH')); // null
Get a random value of the enum.
print(Animal.values.getRandomValue()); // 1