Book Image

DART Cookbook

By : Ivo Balbaert
Book Image

DART Cookbook

By: Ivo Balbaert

Overview of this book

Table of Contents (18 chapters)
Dart Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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 = const IssueDegree('CRITICAL');
}

void main() {
  var issueLevel = IssueDegree.IMPORTANT;
  // Warning and NoSuchMethodError for IssueLevel2:
  // There is no such getter ALARM in IssueDegree
  // var issueLevel2 = IssueDegree.ALARM;...