CodeBlanks Logo Icon CodeBlanks

Summary - Packages

Combine pubspec, pub get and import to set up a package structure.

Summary: Packages

In this group you saw the pub commands, the pubspec.yaml structure, dart pub get, the import statement and dart pub publish. Packages are the foundation of the Dart ecosystem.

Now we combine them: we will import the dart:math and dart:convert libraries and turn a Map into JSON.

Remember: pubspec lists dependencies; pub get fetches; import uses; publish publishes.

Practice max and min JSON

Import the libraries with import 'dart:math'; and import 'dart:convert';. In main pass the Map {'max': max(3, 7), 'min': min(3, 7)} to jsonEncode inside print.

Starter code

main.dart
// Import the dart:math and dart:convert libraries

void main() {
  // print a Map with max and min using jsonEncode
}

Sample solution

main.dart
import 'dart:math';
import 'dart:convert';

void main() {
  print(jsonEncode({'max': max(3, 7), 'min': min(3, 7)}));
}

Expected output

{"max":7,"min":3}

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Concept matching

Term Meaning
pub Package manager
pubspec.yaml Configuration
dart pub get Fetching
import Importing

2 When is dart pub get run?

  • When publishing
  • When dependencies change Correct answer
  • When writing tests
  • When defining a class

Explanation: pub get is run when dependencies change.

3 The import statement

Question
____ 'dart:math';
Answer
import 'dart:math';

Explanation: a library is imported with import.

All lessons in Intermediate