Dart – Emulating enums using Constant Constructor

Whilst Dart doesn’t have support for enum types but it turns out you can do a pretty good job of emulating it using Dart’s constant constructors (which allows you to create compile-time constants with your class, which in .Net you’re restricted to numbers, booleans, strings and null).

Using the technique from this SO answer you will start with a base class for your enum types:

From here, you can create other specialized enum types, such as:

 

Thank you, mr Sergy Akopkokhyants for this elegant little solution.

2 thoughts on “Dart – Emulating enums using Constant Constructor”

  1. Pingback: Introduce raven_dart, a Dart client for Sentry | theburningmonk.com

  2. Pingback: Year in Review, 2014 | theburningmonk.com

Leave a Comment

Your email address will not be published. Required fields are marked *