Visual Studio Code - Target of URI doesn't exist 'package:flutter/material.dart'

220,442

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 !

Share:
220,442

Related videos on Youtube

James
Author by

James

Updated on July 08, 2022

Comments

  • James
    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:

    first line of the code

    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 and flutter 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
      Günter Zöchbauer almost 7 years
      What 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
      James almost 7 years
      It's correct, it says "Dart - 1.24.0-dev.6.7" @GünterZöchbauer
    • James
      James almost 7 years
      But if I go to my flutter project and execute flutter run it installs properly on my device
    • Günter Zöchbauer
      Günter Zöchbauer almost 7 years
      Do you have a non-Flutter Dart SDK installed?
    • Günter Zöchbauer
      Günter Zöchbauer almost 7 years
      I leave it to Danny. I don't really know about vscode.
    • James
      James almost 7 years
      I have flutter sdk in my documents folder, and have dart installed with homebrew
    • Michael Thomsen
      Michael Thomsen almost 7 years
      Does flutter analyze show any issues? If it doesn't, then this sounds like a VSCode specific issue.
    • Kyle Bradshaw
      Kyle Bradshaw almost 7 years
      Is this problem still occurring? If so, does the bottom right version indicator show something like 1.25.0-dev.7.0 (Flutter)?
    • Danny Tuppeny
      Danny Tuppeny over 6 years
      I 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
      mbwasi about 6 years
      Got this same issue but with Android Studio, restarting AS fixes it.
    • Bliv_Dev
      Bliv_Dev about 4 years
      Its 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
      Paresh Mangukiya over 3 years
  • Danny Tuppeny
    Danny Tuppeny over 6 years
    If 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
    Zooly about 6 years
    Notice that it's now flutter packages get
  • Miiite
    Miiite over 5 years
    I'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
    CokoBWare over 5 years
    Unfortunately, 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
    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
    Sravan over 5 years
    I just closed the folder in the VSCode and opened again. this resolved:)
  • user603749
    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
    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
    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
    Chinnon Santos over 4 years
    This worked for me on Ubuntu (flutter packages get and then restarted VSCode). Thanks!
  • Mustafa
    Mustafa about 4 years
    Please 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
    Mahesh Jamdade over 3 years
    Notice that it's now flutter pub get flutter.dev/docs/development/packages-and-plugins/…
  • Tyler2P
    Tyler2P over 3 years
    Note: 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
    buckleyJohnson over 3 years
    Worked on Android Studio too. This belongs as an answer otherwise I wouldn't have seen it.
  • Pramod
    Pramod about 3 years
    Restarting the VSCode runs flutter pub get command which can fix the issue.
  • Mohd Sakib Syed
    Mohd Sakib Syed over 2 years
    Perfect solution
  • Stefan
    Stefan over 2 years
    This worked for me without any sort of restart or reload of VS Code
  • victorkolis
    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
    Ahmed Shendy almost 2 years
    2022 and still works.