Hiprup

What is `pubspec.yaml` and what role does it play in a Flutter project?

pubspec.yaml is the central project manifest — it tells Flutter what dependencies to fetch, what assets to bundle, what fonts to ship, and what Dart/Flutter SDK versions you require.

Sections:

  • name, description, version (1.0.0+42 — semver + buildNumber).

  • environment — Dart SDK + Flutter SDK constraints.

  • dependencies — runtime packages (pub.dev, Git, local path).

  • dev_dependencies — build-time tools (tests, lints, build_runner, freezed) — never shipped in the binary.

  • flutter: — assets, fonts, uses-material-design, etc.

Version syntax: ^1.2.3 = >=1.2.3 <2.0.0 (caret, most common); ~1.2.3 = >=1.2.3 <1.3.0; pinning exact versions causes transitive lockup.

# pubspec.yaml — central project manifest
name: my_app
description: A new Flutter project.
publish_to: 'none'           # don't publish to pub.dev
version: 1.0.0+1             # version+buildNumber (e.g., 1.0.0+42)

environment:
  sdk: '>=3.4.0 <4.0.0'      # Dart SDK constraint
  flutter: '>=3.24.0'        # Flutter SDK constraint

dependencies:
  flutter:
    sdk: flutter
  cupertino_icons: ^1.0.6    # caret = compatible with 1.x.x
  go_router: ^14.0.0
  dio: 5.4.0                 # exact version
  http: '>=1.0.0 <2.0.0'     # range
  my_private_pkg:
    git:                     # from a Git repo
      url: https://github.com/me/my_pkg.git
      ref: main
  local_pkg:
    path: ../local_pkg       # from local path (monorepo)

dev_dependencies:
  flutter_test:
    sdk: flutter
  flutter_lints: ^4.0.0
  build_runner: ^2.4.0
  freezed: ^2.5.0            # codegen — dev-only

flutter:
  uses-material-design: true

  assets:                    # bundled with the app
    - assets/images/
    - assets/data/config.json

  fonts:
    - family: Inter
      fonts:
        - asset: assets/fonts/Inter-Regular.ttf
        - asset: assets/fonts/Inter-Bold.ttf
          weight: 700

# Versioning explained:
#   ^1.2.3   => >=1.2.3 <2.0.0   (caret — most common)
#   ~1.2.3   => >=1.2.3 <1.3.0
#   1.2.3    => exactly 1.2.3 (avoid — pins transitively)
#   any      => any version (avoid — fragile)

The YAML demonstrates every dependency source you'll encounter: pub.dev versions (with caret, tilde, exact, and range syntaxes), Git repos (with ref: to pin a branch or tag), and local path: deps (for monorepos and in-progress packages). The flutter: block declares bundled assets and fonts — these are read at build time and tree-shaken if unused.

dev_dependencies are excluded from the runtime bundle, which is why build_runner and freezed belong there and not in dependencies.

Three details senior interviewers listen for: (1) ^1.2.3 means >=1.2.3 <2.0.0 — the caret is the most common syntax and signals you've read pubspec semantics; (2) dev_dependencies is for tools that don't ship in the binary (test, lints, build_runner) — pinning them keeps your runtime bundle clean; (3) version: 1.0.0+1 — the +1 is the build number, separate from the semver, used by app stores. Add: assets and fonts are declared here too (common gotcha).

What is `pubspec.yaml` and what role does it play in a Flutter project? | Hiprup