Skip to main content
The hide combinator is a namespace modifier used within import and export directives to explicitly exclude specific identifiers from the current scope. It acts as a subtractive filter, allowing all members of a library to be visible except those listed after the keyword.

Syntax

The hide combinator follows the URI string in a directive. Multiple identifiers are separated by commas.
import 'package:library_name/file.dart' hide Identifier1, Identifier2;
export 'package:library_name/file.dart' hide Identifier1, Identifier2;

Mechanics and Resolution

  1. Namespace Filtering: When the Dart compiler processes a directive containing hide, it imports the entire public API of the referenced library but omits the specified symbols from the local namespace.
  2. Compile-Time Enforcement: Attempting to reference a hidden identifier results in a compile-time error, stating that the name is not defined.
  3. Inverse Relationship: hide is the functional inverse of the show combinator. While show is additive (allow-list), hide is subtractive (deny-list).
  4. Chaining: Combinators can be chained, though it is syntactically uncommon. The operations are applied sequentially.

Implementation Example

Consider a library named graphics.dart containing three classes:
// graphics.dart
class Point {}
class Line {}
class Shape {}
When importing this library using the hide combinator:
// main.dart
import 'graphics.dart' hide Line, Shape;

void main() {
  // Valid: Point is imported implicitly because it was not hidden.
  var p = Point();

  // Compile-time Error: The name 'Line' isn't defined.
  var l = Line(); 
  
  // Compile-time Error: The name 'Shape' isn't defined.
  var s = Shape();
}

Export Behavior

When used with the export directive, hide prevents specific members of the re-exported library from becoming part of the current library’s public API.
// utils.dart
// Exports all members of 'graphics.dart' EXCEPT 'Point'.
// Consumers of 'utils.dart' will see Line and Shape, but not Point.
export 'graphics.dart' hide Point;
Master Dart with Deep Grasping Methodology!Learn More