Visual Studio Code - Target of URI doesn't exist 'package:flutter/material.dart'
Solution 1
Restarting Visual Studio Code after
flutter packages get
resolved the error messages for me (previously flutter pub get
)
source: https://flutter.dev/docs/development/packages-and-plugins/using-packages
Solution 2
flutter clean
flutter packages get
flutter packages upgrade ( Optional - use if you want to upgrade packages )
Restart Android Studio or Visual Studio
Solution 3
Author of the Dart Code plugin here! From the screenshot, I would say this is because your flutter project is in a sub-folder of the folder open in Code. We only scan the opened-folder to check whether it's a Flutter project - which then changes the SDK we launch and also enables the Flutter functionality (like the daemon and debugger).
You should re-open the "todo" folder directly.
If you want to have multiple projects open together, use multi-root workspaces, since Dart Code is multi-root-aware and will check each of the folders in the workspace when deciding if it needs to enable Flutter functionality.
Update
This case should be better supported in the Dart plugin now, so opening the immediate parent folder of a Flutter project should work as expected.
Solution 4
If you have used the flutter packages get
command and the error still persists, you can simply reload VS code the Developer: Reload Window
command. Simply type that in after pressing Ctrl+Shift+P
(Cmd+Shift+P
for Mac users). It will clear the error. It's like refreshing VS Code.
Solution 5
Restarting visual studio code worked for me !
Related videos on Youtube
James
Updated on July 08, 2022Comments
-
James almost 2 years
I've just set up my Macbook for flutter development,
So I downloaded flutter SDK, and placed it in my Documents. After, I set up my path variable to work with flutter in my command line.
I execute the command
flutter create todolist
so I achieve a blank project.I also set up my visual studio code with Dart-Code so I have the proper syntax highlight, IntelliSense, etc.
So when I open visual studio code, it highlights the
import
method, like the following image:This means that it also highlights the classes that are correct, making it completely hard to understand if it's a real error or not.
I have executed both
pub get
andflutter packages get
and got everything correctly. I have my path variables all correctly.Why is it highlighting it cannot find that 'import' package?
-
Günter Zöchbauer almost 7 yearsWhat does the status bar say about the Dart version? See also github.com/Dart-Code/Dart-Code/issues/318. See also github.com/Dart-Code/Dart-Code/issues/…
-
James almost 7 yearsIt's correct, it says "Dart - 1.24.0-dev.6.7" @GünterZöchbauer
-
James almost 7 yearsBut if I go to my flutter project and execute flutter run it installs properly on my device
-
Günter Zöchbauer almost 7 yearsDo you have a non-Flutter Dart SDK installed?
-
Günter Zöchbauer almost 7 yearsI leave it to Danny. I don't really know about vscode.
-
James almost 7 yearsI have flutter sdk in my documents folder, and have dart installed with homebrew
-
Michael Thomsen almost 7 yearsDoes
flutter analyze
show any issues? If it doesn't, then this sounds like a VSCode specific issue. -
Kyle Bradshaw almost 7 yearsIs this problem still occurring? If so, does the bottom right version indicator show something like
1.25.0-dev.7.0 (Flutter)
? -
Danny Tuppeny over 6 yearsI just stumbled across this and I know it's been a while, but did you manage to resolve this? Can you reproduce it? I'd love to take a look (I'm the author of Code's Dart/Flutter plugin). My guess is that Dart Code has not realised your project is a Flutter project (which I think @TehSkull was suggesting), which might be because the flutter project is in a sub-folder?
-
mbwasi about 6 yearsGot this same issue but with Android Studio, restarting AS fixes it.
-
Bliv_Dev about 4 yearsIts working for me after restarting VS Code. The command you might need to run is "flutter pub get" but VS Code is even doing this for you.
-
Paresh Mangukiya over 3 yearsTry to this: stackoverflow.com/a/64222311/10563627
-
-
Danny Tuppeny over 6 yearsIf you're able to reproduce this; I'd love to take a look. You shouldn't need to manually restart VS Code to fix issues like this (if you do, I'd like to fix). You can raise issues at github.com/Dart-Code/Dart-Code/issues/new thanks!
-
Zooly about 6 yearsNotice that it's now
flutter packages get
-
Miiite over 5 yearsI've been mumbling for 20 minutes at this package error, moving my installation folder around and stuff like that ... I just opened the wrong folder. I'm a moron. Thank you :)
-
CokoBWare over 5 yearsUnfortunately, as of Flutter 1.0, when using VS Code and following the Flutter.io Get Started tutorial, this lack of package detection is rearing is ugly head again. The plugin properly runs "flutter packages get", but VS Code doesn't see the package and does all of the code underlining as if it were an error across the relevant classes. Restarting VS Code make the error lines go away once I loaded the project folder again. Maybe it's a regression error?
-
Danny Tuppeny over 5 years@CokoBWare Do you mean when using the
Flutter: New Project
command? I just tested it here and once the package update had finished, all red squiggles disappeared. -
Sravan over 5 yearsI just closed the folder in the VSCode and opened again. this resolved:)
-
user603749 about 5 years@DannyTuppeny was fixed for Flutter Projects but not for Dart Projects. Either way for now I just right click on pubspec.yaml and run Get Packages
-
Danny Tuppeny about 5 years@user603749 there's no reason this shouldn't work for Dart too. Please open an issue on GitHub with details of what you see so I can investigate. Thanks!
-
Jay over 4 years(this worked for me) but its very odd that, we still need to do a editor restart just to connect to existing files.
-
Chinnon Santos over 4 yearsThis worked for me on Ubuntu (
flutter packages get
and then restarted VSCode). Thanks! -
Mustafa about 4 yearsPlease add more context to your answer to say how it's different from other answers here and make it more useful in the long run.
-
Mahesh Jamdade over 3 yearsNotice that it's now
flutter pub get
flutter.dev/docs/development/packages-and-plugins/… -
Tyler2P over 3 yearsNote: answers that are very brief and/or are questions back to the poster should probably be comments. You only need 50 rep points to comment under a question - could you move this there?
-
buckleyJohnson over 3 yearsWorked on Android Studio too. This belongs as an answer otherwise I wouldn't have seen it.
-
Pramod about 3 yearsRestarting the VSCode runs
flutter pub get
command which can fix the issue. -
Mohd Sakib Syed over 2 yearsPerfect solution
-
Stefan over 2 yearsThis worked for me without any sort of restart or reload of VS Code
-
victorkolis about 2 years$ flutter clean $ flutter packages get $ flutter packages upgrade If you are running on vscode do it in the debug mode.
-
Ahmed Shendy almost 2 years2022 and still works.