Dart Imports
Dart Imports
A library is a collection of ready-made code, like functions, classes and constants.
Dart comes with many libraries, for example for math, dates, files and JSON.
To use a library, you import it at the top of your file with the import keyword.
The core libraries of Dart all start with dart:, like dart:math.
Import a Library
The dart:math library has math functions and constants, like sqrt() (square root) and pi.
After the import, you can use all the names from the library directly:
Example
import 'dart:math';
void main() {
print(sqrt(16));
print(pi);
print(max(10, 25));
}
Try it Yourself »
Result:
4.0
3.141592653589793
25
If you forget the import, Dart does not know the names, and the program does not run:
Result:
main.dart:2:9: Error: Method not found: 'sqrt'.
print(sqrt(16));
^^^^
Note: The import statements must come before any other code in the file.
The dart:core Library
You have used print(), String, int and List without any import.
That is because they come from dart:core, which Dart imports into every file for you.
Here is a list of some of the core libraries:
| Library | What it contains |
|---|---|
dart:core | Numbers, strings, lists, maps, print() and more (imported automatically) |
dart:async | Tools for async code, like StreamController and Timer |
dart:math | Math functions, constants and random numbers |
dart:convert | Convert data to and from JSON and other formats |
dart:io | Files, folders, user input and network (not in web apps) |
dart:isolate | Run code in isolates |
dart:collection | More collection types, like Queue |
You can import more than one library in the same file:
Example
import 'dart:convert';
import 'dart:math';
void main() {
print(jsonEncode({'name': 'Anna', 'age': 31}));
print(min(4, 9));
}
Try it Yourself »
Result:
{"name":"Anna","age":31}
4
Import Only Some Names With show
If you only need a few names from a library, use show.
Only the names after show are imported:
Example
import 'dart:math' show max, min;
void main() {
List<int> scores = [72, 95, 64];
print(max(scores[0], scores[1]));
print(min(scores[1], scores[2]));
}
Try it Yourself »
Result:
95
64
Leave Out Names With hide
hide does the opposite of show.
It imports every name from the library, except the names after hide:
Result:
9.0
Now Random is not imported, so this program does not run:
Example
import 'dart:math' hide Random;
void main() {
print(Random(1).nextInt(10));
}
Try it Yourself »
Result:
main.dart:4:9: Error: Method not found: 'Random'.
print(Random(1).nextInt(10));
^^^^^^
show and hide are useful when two libraries use the same name for different things, and you only want one of them.
Use a Prefix With as
With as, you give a library a short name, called a prefix.
Then you write the prefix and a dot before each name from the library:
Example
import 'dart:math' as math;
void main() {
double radius = 2;
double area = math.pi * radius * radius;
print('Area: ${area.toStringAsFixed(2)}');
print(math.sqrt(49));
}
Try it Yourself »
Result:
Area: 12.57
7.0
A prefix makes it clear where each name comes from.
It also stops two names from different libraries from mixing up.
Import Your Own Files
You can also put your own code in a separate file, and import it with the file name in quotes.
For example, a file called greetings.dart could have this function:
greetings.dart
String greet(String name) {
return 'Hello, $name!';
}
In main.dart, in the same folder, you import it like this:
main.dart
import 'greetings.dart';
void main() {
print(greet('Mia'));
}
Every Dart file is a library, so your own files work the same way as dart:math, with show, hide and as.
To use code that other people have written, you use packages, which you will learn about in the next chapter.