show and hide Keywords
show and hide Keywords
About
- Selective Import: Control namespace
- API Surface: Minimize exposure
- Performance: Reduce loaded symbols
- Readability: Clear dependencies
Main Topics
-
show Keyword
- Definition: Import only specified members
- Example:
import 'package:collection/collection.dart' show DeepCollectionEquality;
-
hide Keyword
- Definition: Exclude specified members
- Example:
import 'dart:math' hide log;
-
Namespace Pollution
- Definition: Avoiding conflicts
- Example:
import 'package:model_a.dart' show User; import 'package:model_b.dart' show Product;
-
Performance Impact
- Definition: Tree shaking
- Example:
import 'package:large_library.dart' show onlyNeededFunction;
-
Common Patterns
- Definition: Typical usage
- Example:
import 'package:flutter/material.dart' show MaterialApp, Scaffold; import 'package:http/http.dart' hide IOClient;
How to Use
- Minimalism: Import only what’s needed
- Conflicts: Hide problematic members
- Documentation: Note why members are hidden
- Testing: Expose different sets
How It Works
- Filtering: Limits visible identifiers
- Compilation: Affects tree shaking
- Scope: Applies per import
- Analysis: Tools understand restrictions
Example:
// Only import specific widgets
import 'package:flutter/material.dart' show AppBar, Text, Scaffold;
// Hide platform-specific implementations
import 'package:http/http.dart' hide BrowserClient;
Conclusion
The show and hide keywords provide precise control over which library members are brought into scope, helping to prevent naming conflicts, reduce namespace pollution, and improve code clarity by making dependencies explicit.