DriversRecommendedOutdated drivers can make a good PC feel brokenScan driver issues before chasing fixes manually.Scan NowOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix Now×
Skip to content
World desk3 min

Dart Enhanced Enums and Constructor Tear-Offs: What Works

Dart enhanced enums have constructors, but their generative constructors cannot be used as tear-offs. A factory can map input to an existing enum value without creating new instances.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Can you use constructor tear-offs with Dart enhanced enums? Not with a generative enum constructor: Dart only permits it to initialize declared enum values or redirect within the same enum. Use a factory constructor when you need a function-like enum API; it can return an existing value, but it cannot create a new one.

What constructor tear-offs do—and why enums differ

A constructor tear-off is a constructor reference used as a function value. For an ordinary class, you can pass StringBuffer.new where a compatible callback is expected instead of writing a forwarding lambda such as (text) => StringBuffer(text). As the Dart constructors guide puts it, “Lambdas serve as a wrapper for the constructor, whereas a tear-off is the constructor.” Dart constructors guide.

As an Amazon Associate I earn from qualifying purchases.

An enhanced enum can have fields, methods and constructors, but its instances are the finite set of values declared in the enum. A generative constructor initializes those declared values; it is not a general-purpose way to create more enum objects. Consequently, MyEnum.new is not a valid callback reference to a generative enum constructor.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Why the analyzer rejects a generative enum constructor reference

The restriction is explicit in Dart’s analyzer diagnostic: “Generative enum constructors can’t be torn off.” A generative enum constructor may be used to initialize one of that enum’s declared values or for a same-enum redirect, not as an independent function reference. See the analyzer diagnostic and the enhanced-enum specification.

For example, in this conventional enhanced-enum declaration, const Mode(1) initializes a declared value; it does not expose Mode.new as a callback:

enum Mode {
  compact(1),
  expanded(2);

  final int scale;
  const Mode(this.scale);
}

Enhanced enums require language version 2.17 or later. Their instance variables must be final, and generative constructors must be constant. The Dart enum documentation describes those constraints.

Use a factory when a callback should select an existing enum value

A factory constructor can provide constructor-shaped invocation syntax while returning one of the enum’s already declared values. The analyzer documentation gives this minimal example:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
enum E {
  a(0);

  const E(int x);

  factory E.c(int x) => a;
}

E.c is a factory path, not a tear-off of the generative constructor. It returns a, which already exists. In this minimal example the argument does not affect the result; a real factory should use its input for a meaningful mapping where appropriate. It still cannot add enum instances. The distinction and permitted uses are set out in the diagnostic documentation.

For application code, choose the expression that matches the job:

  • The desired value is already known: pass the enum constant itself, such as Mode.compact.
  • A callback receives input and should map it to a declared value: provide a factory constructor and pass that factory reference if its signature fits the callback type.
  • You need to construct arbitrary new objects from callback input: use an ordinary class constructor tear-off, not an enum generative constructor.

A factory does not change the enum’s closed set. It only provides a route to return a member of that set.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Dart 3.13 primary constructors change syntax, not enum construction

The Dart language documentation reflects Dart 3.13.3, and primary constructors offer a shorter declaration style for enhanced enums. For example, the documented primary-constructor form can put a declaring parameter in the enum header:

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
enum Color(int value) {
  red(0),
  green(1),
  blue(2);

  final int value;
}

Primary constructors are implicitly constant for enums. This syntax changes how the declaration is written, not the fixed-instance rule or the restriction on tearing off a generative enum constructor. See Dart primary constructors and the enum documentation. Use this newer syntax only when the project’s Dart language version supports it; the traditional enhanced-enum form remains available from language version 2.17.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

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

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.