> ## 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 Private Method

A private method in Dart is a function associated with an object or library whose visibility is strictly confined to the library (typically the file) in which it is defined. Unlike languages that utilize explicit access modifier keywords (such as `private` or `protected`), Dart enforces privacy lexically. A method is designated as private by prefixing its identifier with an underscore (`_`).

## Lexical Scope and Library Privacy

Dart's privacy model is **library-based**, not class-based. If a method is marked as private, it cannot be invoked from outside the library where it resides. However, any other class, function, or top-level declaration within the exact same library (file) maintains full access to that private method, regardless of class boundaries.

## Syntax

```dart theme={"dark"}
class ExampleClass {
  // Public method: Accessible from any library that imports this class
  void publicMethod() {
    _privateMethod(); // Internal invocation
  }

  // Private method: Accessible only within this specific library (file)
  void _privateMethod() {
    print('Executing private method');
  }
}
```

## Visibility Behavior

**File 1: `library_a.dart`**

```dart theme={"dark"}
class ClassA {
  void _restrictedMethod() {
    print('Private to library_a.dart');
  }
}

class ClassB {
  void accessSiblingPrivate() {
    ClassA instanceA = ClassA();
    // VALID: Class B is declared in the same library as Class A.
    // It bypasses class-level encapsulation due to Dart's library-level privacy.
    instanceA._restrictedMethod(); 
  }
}
```

**File 2: `main.dart`**

```dart theme={"dark"}
import 'library_a.dart';

void main() {
  ClassA instanceA = ClassA();
  
  // INVALID: Compilation error. 
  // The method '_restrictedMethod' isn't defined for the class 'ClassA' 
  // because the invocation occurs outside 'library_a.dart'.
  instanceA._restrictedMethod(); 
}
```

## Inheritance and Overriding

Private methods are not inherited by subclasses if the subclass resides in a different library. If a subclass in a separate file defines a method with the same `_name`, it is treated as an entirely new, distinct method rather than an override of the superclass's private method.

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