set keyword that provides write access to a property from outside its defining library. It functions as a mutator, encapsulating assignment logic while preserving the syntactic interface of a standard variable assignment.
Syntax
A setter is defined using theset keyword followed by the property name and a parameter list containing exactly one argument.
Technical Characteristics
- Keyword: The declaration must utilize the
setmodifier. - Parameters: The method signature must accept exactly one required parameter. Optional positional or named parameters are not permitted.
- Return Type: The return type is strictly
void. While Dart syntax permits explicitly writingvoidbefore thesetkeyword (e.g.,void set value(...)), it is redundant and typically omitted. - Backing Field: Explicit setters are often used to update a private instance variable (conventionally denoted by an underscore
_) that holds the object state. - Implicit Setters: For every public instance variable that is not declared
final, Dart automatically generates an implicit public setter. (Note:constvariables are implicitly static and do not generate instance setters).
Implementation Example
The following example demonstrates an explicit setter that modifies a private backing field.Invocation
Although defined as a method, a setter is invoked using the assignment operator (=) rather than method call syntax (()).
Master Dart with Deep Grasping Methodology!Learn More





