Skip to main content
The part of directive is a structural keyword in Dart used to declare that a specific file is a subordinate component of a larger, enclosing library. It establishes a bidirectional relationship with a parent file that declares the corresponding part directive, allowing multiple physical files to be compiled and evaluated as a single logical library entity.

Syntax

Modern Dart utilizes a URI-based syntax to link the subordinate file back to its parent.
Historically, Dart used a library-name identifier. While still supported for backward compatibility, the URI approach is the standard for modern Dart development.

Technical Mechanics

When a file includes the part of directive, it is subject to strict compilation rules and scoping behaviors:
  1. Shared Lexical Scope: Files connected via part and part of share the exact same library scope. This means private members (variables, classes, or functions prefixed with an underscore _) declared in the parent file are fully accessible within the part of file, and vice versa.
  2. Directive Restrictions: A file containing a part of directive cannot contain its own import, export, or library directives. All dependencies must be resolved through the parent library file.
  3. Bidirectional Linkage: The relationship must be explicitly defined in both directions. The parent file must declare part 'child_file.dart';, and the child file must declare part of 'parent_file.dart';. If the linkage is broken or unidirectional, the Dart analyzer will throw a compilation error.

Code Visualization

The following demonstrates the structural relationship between a parent library and a subordinate part file. Parent File: network_module.dart
Subordinate File: src/network_parser.dart
Tired of Poor Dart Skills? Fix That With Deep Grasping!Learn More