external keyword, the constructor delegates its instantiation logic, memory allocation, and initialization to an external host environment, such as the Dart Virtual Machine (VM), a JavaScript engine, or native code via the Foreign Function Interface (FFI).
Syntax
An external constructor is declared using theexternal modifier before the constructor name. It must be terminated with a semicolon rather than a block body.
Technical Characteristics
- Absence of Implementation: An external constructor cannot have a block body (
{}), an expression body (=>), or an initializer list (: field = value). The declaration acts strictly as a signature contract. - Parameter Restrictions: The Dart language specification explicitly prohibits the use of initializing formals (
this.field) and super-initializing formals (super.field) in external constructors. These constructs represent Dart-level initialization logic, which is strictly the responsibility of the external implementation. - Field Initialization Constraints: Because external generative constructors cannot initialize fields (lacking both initializing formals and initializer lists), a class containing uninitialized
finalor non-nullable instance variables cannot use an external generative constructor. To satisfy Dart’s sound null safety and definite assignment rules, you must use one of the following approaches:- Use an
external factoryconstructor instead of a generative constructor. - Initialize the fields directly at their declaration point.
- Mark the fields themselves as
external(a common pattern in JS interop and FFI).
- Use an
- Compiler Resolution: The Dart compiler expects the implementation to be provided by the underlying runtime. If the runtime fails to link the external declaration to a native implementation, invoking the constructor results in a runtime
NoSuchMethodError. - SDK Patching: Within the Dart SDK, external constructors are frequently paired with the
@patchannotation. Theexternalkeyword declares the API in the public-facing Dart interface, while a separate, platform-specific file provides the@patchimplementation that interfaces directly with C++ or JavaScript. - Factory vs. Generative: The
externalkeyword can be applied to both generative constructors and factory constructors. When applied to a factory, the external environment is responsible for returning an instance that conforms to the class type, rather than just allocating memory for a new instance.
Tired of Poor Dart Skills? Fix That With Deep Grasping!Learn More





