pub get
- Dart course
- Intermediate
- Lesson 96/116
- 5 min
Learn to install packages with pub get.
dart pub get
dart pub get fetches the dependencies listed in pubspec.yaml. It creates a .dart_tool/ folder and a pubspec.lock file. This is done before using packages.
pubspec.lock pins the downloaded versions; your teammates use the same versions. pubspec.yaml states the request, pubspec.lock the real version.
When you add or change a dependency you run dart pub get again.
Version pinning
pubspec.yaml writes the requested version range: ^1.0.0 asks for the newest 1.x. pubspec.lock freezes the actually downloaded version.
This way everyone on the team uses the same version. pubspec.lock is committed in app projects; usually not in libraries.
dart pub upgrade raises the lock; it takes new compatible versions.
Practice dart:math and pow
Import the math library with import 'dart:math';. In main print the result of pow(2, 10).toInt() with print.
Starter code
// Import the dart:math library
void main() {
// print the result of 2 to the power of 10
}Sample solution
import 'dart:math';
void main() {
print(pow(2, 10).toInt());
}Expected output
1024
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 The lock file
pubspec.____
pubspec.lock
Explanation: pubspec.lock pins the versions.
2 What does dart pub get create?
- pubspec.yaml
- README
- pubspec.lock and .dart_tool Correct answer
- main.dart
Explanation: pub get creates pubspec.lock and .dart_tool.
3 pub get parts
| Term | Meaning |
|---|---|
dart pub get |
Fetches |
pubspec.lock |
Pins versions |
.dart_tool |
Tool folder |
pubspec.yaml |
Request list |