Skip to main content
The show combinator is a namespace modifier applied to import and export directives. It functions as an exclusive allowlist, permitting only the explicitly specified identifiers to be visible within the current library scope while suppressing all other top-level members of the referenced library.

Syntax

The combinator follows the URI in the directive, followed by a comma-separated list of identifiers.
import 'package:library_name/file.dart' show Identifier1, Identifier2;
export 'package:library_name/file.dart' show Identifier1, Identifier2;

Operational Semantics

  1. Namespace Filtering: When the Dart compiler processes a directive containing show, it iterates through the top-level symbols of the target library. Only symbols matching the provided identifiers are bound to the current namespace.
  2. Symbol Resolution: Any attempt to reference a symbol from the target library that is not included in the show list results in a compile-time Undefined name error.
  3. Granularity: The combinator applies only to top-level definitions (classes, functions, variables, typedefs, extensions, and mixins). It cannot filter static members or instance members within those definitions.

Implementation Example

Consider a library named math_utils.dart containing three top-level functions:
// math_utils.dart
int add(int a, int b) => a + b;
int subtract(int a, int b) => a - b;
int multiply(int a, int b) => a * b;
When importing this library using the show combinator:
// main.dart
import 'math_utils.dart' show add, multiply;

void main() {
  // Valid: 'add' is explicitly allowed.
  print(add(10, 5)); 

  // Valid: 'multiply' is explicitly allowed.
  print(multiply(10, 5));

  // Compile-time Error: The function 'subtract' is not defined.
  // It was excluded from the namespace by the show combinator.
  // print(subtract(10, 5)); 
}

Export Behavior

When applied to an export directive, show restricts which symbols from the re-exported library are exposed to consumers of the current library.
// public_api.dart
// Only 'add' will be visible to libraries importing public_api.dart.
// 'subtract' and 'multiply' remain hidden.
export 'math_utils.dart' show add;
Master Dart with Deep Grasping Methodology!Learn More