O'Reilly logo

Dart: Scalable Application Development by Ivo Balbaert, Sergey Akopkokhyants, Davy Mitchell

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Creating an enum

Enum does not exist in Dart as a built-in type. Enums provide additional type checking and thus, help enhance code maintainability. So what alternative do we have? Look at the code in project enum, where we want to differentiate the degree of an issue reported to us (we distinguish between the following levels: TRIVIAL, REGU LAR, IMPO RTANT, and CRITICAL).

How to do it...

The first way to achieve the creating an enum functionality is shown in enu m1.dart:

class IssueDegree {
  final _value;
  const IssueDegree(this._value);
  toString() => 'Enum.$_value';

static const TRIVIAL = const IssueDegree('TRIVIAL');
static const REGULAR = const IssueDegree('REGULAR');
static const IMPORTANT = const IssueDegree('IMPORTANT');
static const CRITICAL ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required