A public setter in Dart is a specialized method that provides write access to an object’s property. By default, any setter defined without a leading underscore (Documentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
_) is public, meaning it is accessible from anywhere the object’s library is imported. Setters are invoked using the assignment operator (=) rather than standard method invocation syntax.
Dart implements setters in two ways: implicitly and explicitly.
Implicit Public Setters
Whenever you declare a mutable, non-final public instance variable, the Dart compiler automatically generates an implicit public setter for it.Explicit Public Setters
An explicit public setter is defined using theset keyword. It is typically used to intercept the assignment operation to execute logic before mutating a private backing field.
Syntax
Technical Constraints and Characteristics
- Return Type: Setters do not return a value. While explicitly declaring a
voidreturn type (e.g.,void set propertyName(...)) is valid syntax and will compile, omitting the return type entirely is the recommended Dart style convention and is enforced by theavoid_return_types_on_setterslint rule. - Parameters: A setter must accept exactly one required positional parameter. It cannot accept optional, named, or multiple parameters.
- Naming: A setter cannot share a name with a method, variable, or getter in the same class, except for its corresponding getter. It is conventionally named after the logical property it mutates, while the actual data is stored in a private backing variable (prefixed with
_). - Invocation: Setters are called via property access syntax, not method syntax.
Implementation Example
Arrow Syntax
For setters containing a single expression, Dart supports the concise arrow (=>) syntax.
Master Dart with Deep Grasping Methodology!Learn More





