import
- Dart course
- Intermediate
- Lesson 97/116
- 5 min
Learn to import packages with import.
The import statement
The import statement brings another library into your code. Core libraries are written import 'dart:math';, packages import 'package:http/http.dart';.
With show you take only some names, with hide you hide some names. With as you give a prefix: import 'dart:math' as m;.
Imported names become available in the file scope. Two same-named symbols are separated with as.
show, hide and as
With show you take only specific names from a library: import 'dart:math' show max;. With hide you hide some names.
With as you give a prefix: import 'dart:math' as m;. Then you write m.max(3, 7). This prevents name clashes.
These combinators keep code tidy; they stop unneeded names from entering scope.
Practice max and min
Import the library with import 'dart:math';. In main print the results of max(5, 9) and min(5, 9) on a single line, separated by a space, with print.
Starter code
// Import the dart:math library
void main() {
// print the max and min of 5 and 9
}Sample solution
import 'dart:math';
void main() {
print('${max(5, 9)} ${min(5, 9)}');
}Expected output
9 5
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What kind of import is package:http/http.dart?
- Core library
- Relative file
- System library
- Package import Correct answer
Explanation: the package: prefix marks a package import.
2 Importing dart:math
import '____';
import 'dart:math';
Explanation: dart:math is imported this way.
3 Import forms
| Term | Meaning |
|---|---|
import 'dart:math'; |
Core library |
import 'package:http/http.dart'; |
Package |
show |
Name selection |
as |
Prefix |