Enhanced Enums in Dart: Bridging External Data and Application Logic An enum is a type that repre...Enhanced Enums in Dart: Bridging External Data and Application Logic An enum is a type that repre...
The network for creativity
Join 1.25M professional creatives like you
Connect with clients, get discovered, and run your business 100% commission-free
Creatives on Contra have earned over $150M and we are just getting started
Enhanced Enums in Dart: Bridging External Data and Application Logic
An enum is a type that represents a fixed set of related values.
For example, an app might have three possible states: loading, success, failure
Instead of representing these as arbitrary strings throughout the app, an enum gives us a defined set of values that the application can work with.
But Dart takes enums a step further with enhanced enums.
Enhanced enums allow us to attach additional information and behavior to enum values. They can have fields, constructors, methods, and custom logic.
This becomes particularly useful when the values our application works with come from outside the application.
As developers, we regularly receive data from APIs, WebSockets, databases, or third-party services.
And these external systems commonly represent certain values as strings.
For example, an API might return status as strings: "pending" "completed" "cancelled", "failed"
A straightforward approach is to pass these strings through the application and compare them whenever we need to determine what they represent.
That works! But as an application grows, the same strings can start appearing in multiple places.
Now we have repeated string comparisons, possible typos, and business logic that is directly tied to the external system's representation of the data.
This is where enhanced enums can provide a useful bridge.
Recently, I was working on a real-time feature for a card customization experience.
The idea was to allow multiple users to work on the same card together. While editing, users could see each other's activities in real time and see changes to the card as they happened.
To make this possible, the application communicated with the backend through a WebSocket connection.
The backend could (via the websocket) send or receive different types of events depending on what was happening.
For example: "presence.sync" "draft.update"
Rather than allowing these raw strings to flow throughout the application, I modelled the events using an enhanced enum.
Each enum value holds the corresponding string used by the backend.
I then added a fromJson factory that takes the incoming WebSocket string and maps it to the appropriate enum value.
So when the backend sends: "draft.update", I convert it into: RealTimeEvent.cardDraftUpdate
Also, if I wish to indicate that a user want's to join, I can send RealTimeEvent.presenceSynce.value
From that point on, the application works with a known enum value instead of repeatedly comparing raw strings.
This creates a simple boundary:
Websocket → Raw String → Enhanced Enum → Application Logic
The websocket can continue communicating using the format it understands, while the application works with types that make sense within the codebase.
That's what I like about this approach.
The enhanced enum isn't just storing extra information. It becomes the translation layer between an external system and the application's domain.
A relatively small Dart feature, but one that can make working with external data cleaner, safer, and easier to maintain.
Post image
Back to feed
The network for creativity
Join 1.25M professional creatives like you
Connect with clients, get discovered, and run your business 100% commission-free
Creatives on Contra have earned over $150M and we are just getting started