Skip to main content
The part directive is a compilation-level mechanism that aggregates multiple Dart source files into a single library unit. It instructs the compiler to include the content of referenced files into the parent library, treating the code as if it were defined within the same physical file. Files connected via part and part of share a single library scope and private namespace. This allows mutual access to private members (identifiers prefixed with _) across the parent file and all its parts.

Syntax Overview

The relationship requires a bidirectional reference: the parent file declares the part, and the child file declares it is part of the parent. Parent File (Library Entry Point)
// library_main.dart
library my_system; // Optional library declaration

import 'dart:math';

// Declares that 'helper_functions.dart' belongs to this library
part 'helper_functions.dart'; 

void main() {
  // Can access functions and private members defined in the part file
  var result = _calculateHiddenValue(10);
}
Child File (Part)
// helper_functions.dart

// Declares that this file belongs to 'library_main.dart'
part of 'library_main.dart'; 

// Private function is visible to the parent file and other parts
int _calculateHiddenValue(int value) {
  return value * 2;
}

Directives

The part Directive

Located in the main library file, this directive specifies a URI pointing to a subordinate file.
  • Placement: Must occur after the library directive (if present) and after all import and export directives, but before any code declarations (classes, functions, variables).
  • Argument: A string literal representing the URI (relative or absolute) of the file to include.

The part of Directive

Located in the subordinate file, this directive specifies the library to which the file belongs.
  • Placement: Must be the first directive in the file (excluding comments and metadata annotations).
  • Modes:
    1. URI-based (Preferred): Points to the file path of the parent library (e.g., part of 'library_main.dart';).
    2. Name-based (Legacy): Points to a named library defined in the parent (e.g., part of my_system; requires library my_system; in the parent).

Scoping and Namespace

  1. Shared Private Scope: Unlike import, which isolates private members to their specific file, part files share the same private namespace. A class defined in a part file can access private variables defined in the parent file, and vice versa.
  2. Single Compilation Unit: The Dart compiler treats the parent file and all its parts as a single compilation unit.
  3. Dependency Management:
    • No Imports in Parts: Files declared as a part cannot contain import or export directives.
    • Inherited Scope: All dependencies required by the part file must be imported in the parent library file. The part file inherits the import scope of the parent.

Resolution Rules

  • One-to-Many: A library file can have multiple part directives.
  • Many-to-One: A part file can only be part of exactly one library.
  • Transitivity: Parts cannot have parts. A file declared as a part of another library cannot utilize the part directive itself.
Master Dart with Deep Grasping Methodology!Learn More