public, private, or protected. Instead, visibility is determined by library-level privacy based on naming conventions: all methods—including getters and setters—are implicitly public by default unless their identifier is prefixed with an underscore (_).
Syntax
A public method is declared using an optional return type (which implicitly defaults todynamic if omitted), an identifier (starting with a letter or a dollar sign $), and a parameter list enclosed in parentheses. The declaration is concluded with a method body (using a block {} or an arrow =>), or it is terminated with a semicolon (;) if the method is abstract or external.
Public getters and setters follow the same naming rules but use the get and set keywords. Getters omit the parameter list entirely, while setters require exactly one parameter.
Technical Characteristics
- Implicit Visibility: The absence of the
_prefix is the sole mechanism for declaring a method as public. Dart compilers and analyzers treat any non-underscored method, getter, or setter as part of the public API of the enclosing declaration. - Library-Level Privacy: Dart enforces encapsulation at the library level, not the class level. A public method is exposed across library boundaries, meaning it can be invoked by any external Dart file that imports the library containing the method’s declaration.
- Getters and Setters (Accessors): Accessor methods are subject to the exact same visibility rules as standard methods. They form a fundamental part of a class’s public API and implicit interface, allowing property access semantics while executing method logic.
- Implicit Interfaces: Every class in Dart implicitly defines an interface containing all of its instance members, including public methods, private methods, getters, and setters. When a class implements another class using the
implementskeyword, it must provide concrete implementations for the entire interface. Because the implicit interface includes private members, a class containing private methods cannot be fully implemented by a class residing in a different library. - Inheritance and Overriding: Public instance methods defined on classes and mixins are inherited by subclasses and can be overridden using the
@overrideannotation. Conversely, methods defined onextensionandextension typedeclarations are statically resolved and cannot be overridden. When overriding a method, the signature does not need to strictly match the superclass’s definition:- Covariant Return Types: Dart allows an overridden method to return a subtype of the original return type.
- Contravariant Parameter Types: Dart naturally supports contravariance for parameters, allowing an overridden method to safely accept a supertype of the original parameter type without any special keywords.
- Covariant Parameters: If an overridden method needs to accept a subtype of the original parameter type, the parameter must be explicitly marked with the
covariantkeyword.
- Invocation: Public instance methods are invoked on an instantiated object or value using dot notation (
instance.methodName()). Public static methods are invoked directly on the type declaration itself (ClassName.methodName()). Public getters and setters are invoked using property access syntax (instance.propertyNameandinstance.propertyName = value).
Tired of Poor Dart Skills? Fix That With Deep Grasping!Learn More





