Pragma annotation is a metadata directive used to provide implementation-specific hints to the Dart compiler, runtime, or static analysis tools. It allows developers to influence toolchain behavior—such as compilation strategies or tree-shaking logic—without altering the semantic logic defined by the Dart language specification.
Class Definition
The annotation is an instance of thePragma class, defined in dart:core.
Syntax
The annotation is applied as a prefix to declarations (classes, fields, methods, or functions) by invoking theconst constructor of the Pragma class. It requires a name string and accepts an optional options object.
Parameters
-
name(String): A key string identifying the specific hint. This string must match a directive recognized by the target tool (e.g., the Dart VM,dart2js, or the analyzer). Conventionally, these strings are namespaced (e.g.,vm:entry-point,dart2js:noInline). -
options(Object?): An optional payload providing additional context or configuration for the directive. The expected type and structure of this object depend entirely on the specificnameprovided and the tool consuming it.
Resolution Mechanics
- Tool-Specific: The behavior triggered by a
Pragmais defined entirely by the implementation of the compiler or runtime processing the code. - Ignored by Default: If a tool encounters a
Pragmawith anameit does not recognize, the annotation is ignored silently. It does not produce compilation errors or runtime exceptions. - Multi-Targeting: Because unrecognized pragmas are ignored, a single declaration may carry multiple
Pragmaannotations targeting different tools (e.g., one for the VM and one for the web compiler) without conflict.
Master Dart with Deep Grasping Methodology!Learn More





