Enumerated Types #1

This lesson introduces Enumerated Types in Dart.

Introduction

In Dart 1.8 release, enumerated Types (a.k.a. Enums) were added as an experimental feature.

Enums are like a class that represent a fixed number of constant values. For example, you have an app that fetches data from a remote server. Depending on the type of response received from the server, the app shows one of the following statuses:

  • done: The app received the response successfully.
  • waiting: The app is waiting for the response.
  • error: The app received an error from the server.

The above three responses can be declared using the enum keyword below:

enum Status {
  done,
  waiting,
  error, //This comma is optional
}

Note: The comma trailing the last entry (ERROR) is optional.

Use UpperCamelCase for Enums like Status.

Use lowerCamelCase for Enum values. For example: done, waiting, and error.

Get hands-on with 1400+ tech skills courses.