Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Working with Assets  ›  Lesson

Images

Images 12 minImage files must be declared under flutter assets in pubspec.yaml before Image.asset can load them, and resolution folders such as 2.0x let Flutter pick a sharper version for denser screens.

Images

Sprout should show a small photo of each plant: a fern, a cactus, a snake plant. Those pictures ship inside the app, so they work offline. Flutter only bundles files you name in pubspec.yaml; anything else in the project folder is left out of the build.

The rule

Declare it in pubspec, then load it by the same path.

flutter:
  uses-material-design: true
  assets:
    - assets/images/
    - assets/images/plants/
Image.asset('assets/images/plants/fern.png', width: 64, height: 64)
  • Paths are relative to the project root, and the one in Image.asset must match the file exactly, including case.
  • Listing a folder (ending in /) includes every file directly inside it, but not its subfolders. Each subfolder needs its own line.
  • YAML is strict about indentation: assets: sits under flutter:, and each entry is a dash one level deeper.
  • After editing pubspec.yaml, stop and rerun the app. A hot reload does not pick up new asset entries.

Sharper images on dense screens. Add higher-resolution copies in folders named by pixel ratio:

assets/images/plants/fern.png        (1x)
assets/images/plants/2.0x/fern.png
assets/images/plants/3.0x/fern.png

Keep loading 'assets/images/plants/fern.png'. On a phone with a 3x screen, Flutter picks the 3.0x copy automatically.

Images from the internet use Image.network(url) instead, and need no pubspec entry, but they need a connection.

The shortcut

When an image will not show, walk the path from file to pubspec to code, and stop at the first mismatch.

Debugging a missing cactus photoStep 1 of 4
  1. File

    Is it really at assets/images/plants/cactus.png? Check spelling and case: Cactus.PNG is a different file.

The trap is assuming a folder entry covers everything beneath it. It covers one level only.

Drills

Answer fast — trust the rule.

quick drill

pubspec.yaml lists - assets/images/. Is assets/images/plants/fern.png bundled?

quick drill

Which widget shows a picture bundled with the app?

quick drill

You add a new assets entry to pubspec.yaml while the app runs. What next?

quick drill

A 2x phone loads 'assets/images/plants/fern.png' and a 2.0x/fern.png exists beside it. Which file is used?

Remember

  • Declare it in pubspec, then load it by the same path.
  • Debug from file to pubspec to code, then restart.
  • Trap: a folder entry does not include its subfolders.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainWhy is the photo missing?

You put fern.png in assets/images/plants/ and call Image.asset('assets/images/plants/fern.png'), but the app shows an 'Unable to load asset' error. pubspec.yaml lists '- assets/images/'. Explain the cause and the fix, and what you must do after editing pubspec.yaml.