CodeBlanks Logo Icon CodeBlanks

pubspec.yaml

Learn the pubspec.yaml configuration.

The pubspec.yaml structure

pubspec.yaml is the configuration file of a Dart package. It holds the package name (name), version (version) and Dart SDK constraint (environment).

Dependencies go in the dependencies section: http: ^1.0.0. Development dependencies go in dev_dependencies; like the test package.

YAML is indentation sensitive; use two spaces. Not tabs.

dev_dependencies

dependencies are for runtime dependencies; required for the program to run. dev_dependencies are needed only during development; test and code generation tools.

For example the test package goes in dev_dependencies; it does not run in production. http goes in dependencies.

When publishing dev_dependencies are not fetched; the package stays smaller.

Practice dart:convert and jsonEncode

Import the convert library with import 'dart:convert';. In main print the result of jsonEncode({'name': 'Dart', 'version': '3'}) with print.

Starter code

main.dart
// Import the dart:convert library

void main() {
  // print a Map with jsonEncode
}

Sample solution

main.dart
import 'dart:convert';

void main() {
  print(jsonEncode({'name': 'Dart', 'version': '3'}));
}

Expected output

{"name":"Dart","version":"3"}

Questions and answers

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

1 In which section are dependencies?

  • name
  • dependencies Correct answer
  • version
  • environment

Explanation: dependencies go in the dependencies section.

2 The dependencies section

Question
dependencies:
  ____: ^1.0.0
Answer
dependencies:
  http: ^1.0.0

Explanation: the http package goes under dependencies.

3 pubspec parts

Term Meaning
name: Package name
version: Version
environment: SDK constraint
dependencies: Runtime dependencies

All lessons in Intermediate