Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Flutter and app store releases

VS Code underlines package:flutter/material.dart as a missing target

ErrorTarget of URI doesn't exist: 'package:flutter/material.dart'.

Short answer

The analyzer found no package map, so package imports cannot resolve. Run flutter pub get to create it, open the folder that contains pubspec.yaml as the workspace, and make sure the Dart extension can find your Flutter SDK. Then the red lines go away without touching the code.

  • Applies to Flutter 3.3x stable, Dart 3.x, VS Code with the Flutter and Dart extensions
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Check whether pub ever wrote a package map for this project. Imports resolve through this file.

    ls -l .dart_tool/package_config.json
  2. 2Match what you see, then jump to its fix

    1. 1.dart_tool/package_config.json is missing or oldDependencies were never fetched, or the fetch failedFix
    2. 2No Flutter version or device in the status barVS Code opened a parent folder, not the projectFix
    3. 3Extension complains it cannot find the SDK, or analysis never startsThe Dart extension cannot find the Flutter SDKFix

What does the error look like?

  • Target of URI doesn't exist: 'package:flutter/material.dart'.
  • Undefined class 'StatelessWidget'.

Cause 1 of 3

Dependencies were never fetched, or the fetch failed

1Check

pub get reads pubspec.yaml and writes .dart_tool/package_config.json, which maps every package name to a folder. Without it the analyzer knows no packages.

flutter pub get

2Fix

Fix any error pub get prints (a syntax slip in pubspec.yaml, no network), then run it again. In VS Code you can also click Get Packages at the top of pubspec.yaml.

flutter pub get

3Verify

The file exists and the import loses its underline.

ls -l .dart_tool/package_config.json

Confirmed by dart.dev

Did this fix it?

Cause 2 of 3

VS Code opened a parent folder, not the project

1Check

The folder that holds pubspec.yaml must be a workspace folder. If you opened a folder one level above, the project is not detected as a Flutter project.

ls pubspec.yaml

2Fix

Use File, Open and choose the folder that directly contains pubspec.yaml.

code /path/to/your_app

3Verify

The status bar shows the Flutter version and a device.

# VS Code: Command Palette > Flutter: Run Flutter Doctor

Confirmed by docs.flutter.dev

Did this fix it?

Cause 3 of 3

The Dart extension cannot find the Flutter SDK

1Check

If dart.flutterSdkPath is empty or wrong, the extension searches the project, then the FLUTTER_ROOT variable, then PATH.

which flutter
echo $FLUTTER_ROOT

2Fix

Set the SDK path in VS Code settings with ~ for the home folder and forward slashes, then reload the window.

// settings.json
{ "dart.flutterSdkPath": "~/development/flutter" }

3Verify

Flutter: Run Flutter Doctor prints the Flutter version in the Output panel.

flutter doctor -v

Confirmed by dartcode.org

Did this fix it?

How do I stop it coming back?

  • Run flutter pub get right after cloning or switching branches that change pubspec.yaml.
  • Open one Flutter project per VS Code window, at its root.
  • Keep the Flutter SDK in a stable folder and set its path once in user settings.
  • Do not commit .dart_tool; regenerate it with pub get.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.