The enum type
The enum
type is a common data type that’s used by most languages to represent a set of finite constant values. In Dart, it is no different. By using the enum
keyword, followed by the constant values, you can define an enum
type, as illustrated in the following code snippet:
enum PersonType { student, employee }
Note that you only define the value names. enum
types are special types with a set of finite values that have an index property representing their value. Now, let’s see how it all works.
First, let’s add a field to our previously defined Person
class so that we can store its type, as follows:
class Person { ... PersonType type; Person(this.type); ... }
Then, we can use it just like any other field, as illustrated in the following code snippet:
main() { print(PersonType.values); Person somePerson = (PersonType.student); somePerson.type = PersonType.employee; print(somePerson...