@ symbol that evaluates to a compile-time constant.
Syntax and Placement
Annotations are placed immediately preceding the declaration they modify, such as a library, class, function, variable, or parameter. The syntax varies depending on whether the annotation is a reference to a constant variable or an invocation of a constant constructor. 1. Constant Variable Reference If the annotation is a defined constant variable, parentheses are omitted.Defining Custom Annotations
Any object that is a compile-time constant can serve as an annotation. There are two primary ways to define them:Class-Based Annotations
The most common approach is to define a class with aconst constructor. This allows the annotation to accept parameters and structure data.
Constant Variable Annotations
You can also define a top-level constant variable (such as a string, number, or boolean) and use it directly as an annotation.Parameter Constraints
Because annotations are evaluated at compile-time, arguments passed to an annotation constructor must be compile-time constants. Valid arguments include:- Literals (numbers, strings, booleans, null).
- Const lists, sets, or maps.
- References to other constant variables.
- Invocations of other constant constructors.
Restricting Targets
By default, an annotation can be applied to any declaration. To restrict an annotation to specific code elements (e.g., only classes or only parameters), apply the@Target meta-annotation from package:meta.
Processing Mechanisms
Dart annotations do not have inherent executable behavior; they are passive data. They are utilized through two primary mechanisms:- Static Analysis: Tools like the Dart analyzer read annotations to enforce rules (e.g.,
@override,@deprecated). - Code Generation/Reflection:
- Compile-time: Build systems (using
package:source_genandpackage:build_runner) scan source code for specific annotations to generate additional files (e.g., JSON serialization logic). - Runtime: Reflection (via
dart:mirrors, though generally discouraged or unavailable in Flutter/Web) allows introspection of metadata during execution.
- Compile-time: Build systems (using
Master Dart with Deep Grasping Methodology!Learn More





