VS Code underlines package:flutter/material.dart as a missing target
Target 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.
Fix it fast
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.json2Match what you see, then jump to its fix
- 1
.dart_tool/package_config.json is missing or oldDependencies were never fetched, or the fetch failedFix - 2
No Flutter version or device in the status barVS Code opened a parent folder, not the projectFix - 3
Extension complains it cannot find the SDK, or analysis never startsThe Dart extension cannot find the Flutter SDKFix
- 1
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 get2Fix
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 get3Verify
The file exists and the import loses its underline.
ls -l .dart_tool/package_config.jsonConfirmed by dart.dev
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.yaml2Fix
Use File, Open and choose the folder that directly contains pubspec.yaml.
code /path/to/your_app3Verify
The status bar shows the Flutter version and a device.
# VS Code: Command Palette > Flutter: Run Flutter DoctorConfirmed by docs.flutter.dev
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_ROOT2Fix
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 -vConfirmed by dartcode.org
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:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- Qist BazaarA buy-now-pay-later marketplace built from a blank repo, now past 100K downloads on Google Play.
- SessionBirdAn omnichannel engagement suite in Flutter: campaigns, chatbot, tickets, analytics and live calling.
Related fixes
Mobile apps
AudioCache has no play method after updating audioplayers
The method 'play' isn't defined for the type 'AudioCache'.
Since audioplayers 1.0, AudioCache only turns asset files into local files and caches them.
2 causes, each with a fixMobile apps
Flutter Bloc throws that emit was called after the event handler completed
emit was called after an event handler completed normally.
Each on<Event> handler gets its own Emitter, and it stops working the moment the handler's returned Future completes.
2 causes, each with a fixMobile apps
analysis_options.yaml: the URI it includes can't be found
include_file_not_found
The analyzer reports include_file_not_found when the file named after include: in analysis_options.yaml does not resolve.
2 causes, each with a fixMobile apps
Dart error: the body might complete normally and return null for a non-nullable type
body_might_complete_normally
Dart found a path through your function or callback that reaches the closing brace without a return.
3 causes, each with a fixMobile apps
Dart throws ConcurrentModificationError while looping over a list
ConcurrentModificationError
Dart throws this when a collection changes size while something iterates it.
2 causes, each with a fixMobile apps
Dart cannot turn a compact timestamp like 20201101153000 into a DateTime
FormatException: Invalid date format
DateTime.parse accepts a subset of ISO 8601, and a time part must be introduced by T or a space.
2 causes, each with a fix