enum_plus
Simple, extensible and powerful enumeration implementation
Usage
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 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()
equal
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
notEqual
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
inside
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
outside
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
getFriendlyName
Transform the name into a friendly, formatted version.
print(Animal.HONEY_BEE.getFriendlyName()); // Honey Bee
Enum List Methods
hasName
Check that the enum contains a specific name.
print(Animal.values.hasName('DOG')); // true
print(Animal.values.hasName('FISH')); // false
hasFriendlyName
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
getNames
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]
getName
Get the name for a single enum value.
print(Animal.values.getName(1)); // CAT
print(Animal.values.getName(100)); //Bad state: No element
getNamesExcept
Return names of all the enums except the given values.
print(Animal.values.getNamesExcept([0, Animal.HONEY_BEE])); // [CAT]
getFriendlyNames
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]
getFriendlyNamesExcept
Return friendly names of all the enums except the given values.
print(Animal.values.getFriendlyNamesExcept([Animal.DOG, 1])); // [Honey Bee]
fromNames
Get enums from names.
print(Animal.values.fromNames(['DOG', 'CAT'])); // [Animal.DOG, Animal.CAT]
print(Animal.values.fromNames(['CAT', 'FISH'])); // Bad state: No element
fromFriendlyNames
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
fromName
Make an enum instance from a given key.
print(Animal.values.fromName('DOG')); // Animal.DOG
print(Animal.values.fromName('FISH')); // Bad state: No element
fromFriendlyName
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
toListExcept
Return instances of all the enums except the given values.
print(Animal.values.toListExcept([Animal.DOG, 1])); // [Animal.HONEY_BEE]
getRandom
Get a random instance of the enum.
print(Animal.values.getRandom()); // Animal.HONEY_BEE
getRandomName
Get a random name of the enum.
print(Animal.values.getRandomName()); // DOG
getRandomFriendlyName
Get a random friendly name of the enum.
print(Animal.values.getRandomFriendlyName()); // Cat
hasValue
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
getValues
Get all or a custom set of the enum values.
print(Animal.values.getValues()); // [0, 1, 2]
print(Animal.values.getValues(names: ['DOG'])); // [0]
getValue
Get the value for a single enum key.
print(Animal.values.getValue('DOG')); // 0
print(Animal.values.getValue('FISH')); // Bad state: No element
getValuesExcept
Return values of all the enums except the given values.
print(Animal.values.getValuesExcept([Animal.DOG, 1])); // [2]
fromValue
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
fromValues
Return instances from enum values.
print(Animal.values.fromValues([0, Animal.CAT])); // [Animal.DOG, Animal.CAT]
coerce
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
getRandomValue
Get a random value of the enum.
print(Animal.values.getRandomValue()); // 1