Skip to main content
A public method in Dart is a function bound to a class, mixin, enum, extension, or extension type that is accessible from any library that imports its defining library. Unlike many object-oriented languages, Dart does not utilize explicit access modifier keywords such as 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 to dynamic 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.
abstract class NetworkClient {
  // Public instance method with explicit return type and body
  void connect(String endpoint) {
    // Implementation
  }

  // Public method with omitted return type (implicitly dynamic)
  processData(data) {
    // Implementation
  }

  // Public abstract method (no body, terminated with a semicolon)
  void disconnect();

  // Public external method (implementation provided outside Dart)
  external void nativeCall();

  // Public static method
  static bool validateConfiguration() => true;

  // Public getter (abstract)
  bool get isConnected;

  // Public setter (abstract)
  set connectionTimeout(int seconds);

  // Private method (for contrast; restricted to the defining library)
  void _initializeSocket() {
    // Implementation
  }
}

enum ConnectionState {
  connected, disconnected;
  
  // Public method on an enum
  bool isConnected() => this == ConnectionState.connected;
}

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 implements keyword, 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 @override annotation. Conversely, methods defined on extension and extension type declarations 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 covariant keyword.
  • 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.propertyName and instance.propertyName = value).
Tired of Poor Dart Skills? Fix That With Deep Grasping!Learn More