> ## 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.

# Dart Public Getter

A public getter in Dart is a specialized method that provides read access to an object's state or computed properties using field-access syntax. In Dart, identifiers are public by default; therefore, a getter is public as long as its name does not begin with an underscore (`_`).

## Implicit Public Getters

When you declare a public instance variable, Dart automatically generates an implicit public getter for it.

```dart theme={"dark"}
class User {
  String username = 'admin'; // Implicit public getter generated
}

void main() {
  final user = User();
  print(user.username); // Invokes the implicit getter
}
```

## Explicit Public Getters

Explicit public getters are defined using the `get` keyword. They allow you to define custom logic for property retrieval, such as computing a value dynamically or exposing a private field, while maintaining the syntactic footprint of a standard field access.

### Syntax

An explicit getter is declared with an optional return type, the `get` keyword, and the identifier, followed by either a block body or an arrow function. It strictly takes no parameters and omits the parameter list parentheses `()`.

**Arrow Syntax:**

```dart theme={"dark"}
ReturnType get propertyName => expression;
```

**Block Body Syntax:**

```dart theme={"dark"}
ReturnType get propertyName {
  // computation
  return value;
}
```

### Code Example

```dart theme={"dark"}
class Rectangle {
  double width;
  double height;
  
  // Private variable
  final double _borderWidth = 2.0;

  Rectangle(this.width, this.height);

  // Explicit public getter (Computed property)
  double get area => width * height;

  // Explicit public getter (Exposing private state)
  double get border => _borderWidth;
}

void main() {
  final rect = Rectangle(10.0, 5.0);
  
  // Accessed without parentheses, identical to field access
  print(rect.area);   
  print(rect.border); 
}
```

## Technical Characteristics

* **Parameterless:** A getter cannot define any parameters. Attempting to add parentheses `()` to a getter declaration results in a compilation error.
* **Uniform Access Principle:** Because both implicit and explicit getters are invoked using dot notation (`object.property`), you can change a standard public field into an explicit getter later without breaking the API or requiring changes to the calling code.
* **Scope:** Public getters can be declared at the top level of a library, or within classes, mixins, enums, and extensions.
* **Read-Only Enforcement:** Defining an explicit getter without a corresponding explicit setter (`set`) creates a read-only property from the perspective of the caller. Attempting to assign a value to it will result in a compilation error.
* **Overriding:** Explicit getters can override implicit getters (fields) from a superclass, and vice versa.

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor Dart Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
