Skip to main content

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.

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.
// Modern URI-based syntax (Recommended)
part of 'parent_library.dart';
Historically, Dart used a library-name identifier. While still supported for backward compatibility, the URI approach is the standard for modern Dart development.
// Legacy library-name syntax
part of my_library_name;

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
// Optional library declaration
library network_module;

// All imports must reside in the parent file
import 'dart:convert';
import 'package:http/http.dart' as http;

// Link to the subordinate file
part 'src/network_parser.dart';

// Private member accessible by the part file
String _baseUrl = "https://api.example.com";

class NetworkClient {
  void fetch() {
    // Can access members defined in the part file
    _parseResponse(); 
  }
}
Subordinate File: src/network_parser.dart
// Link back to the parent file using a relative URI
part of '../network_module.dart';

// No imports are allowed here

void _parseResponse() {
  // Accesses the private variable from network_module.dart
  print("Parsing data from $_baseUrl");
  
  // Accesses the dart:convert library imported in the parent
  jsonDecode('{"status": "ok"}'); 
}
Master Dart with Deep Grasping Methodology!Learn More