263

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?

James
  • 3,580
  • 3
  • 25
  • 36
  • What does the status bar say about the Dart version? See also https://github.com/Dart-Code/Dart-Code/issues/318. See also https://github.com/Dart-Code/Dart-Code/issues/198#issuecomment-310620677 – Günter Zöchbauer Jul 04 '17 at 15:35
  • It's correct, it says "Dart - 1.24.0-dev.6.7" @GünterZöchbauer – James Jul 04 '17 at 15:49
  • But if I go to my flutter project and execute ***flutter run*** it installs properly on my device – James Jul 04 '17 at 15:56
  • Do you have a non-Flutter Dart SDK installed? – Günter Zöchbauer Jul 04 '17 at 15:57
  • I leave it to Danny. I don't really know about vscode. – Günter Zöchbauer Jul 04 '17 at 15:58
  • I have flutter sdk in my ***documents*** folder, and have dart installed with homebrew – James Jul 04 '17 at 16:02
  • Does `flutter analyze` show any issues? If it doesn't, then this sounds like a VSCode specific issue. – Michael Thomsen Jul 06 '17 at 08:13
  • Is this problem still occurring? If so, does the bottom right version indicator show something like `1.25.0-dev.7.0 (Flutter)`? – Kyle Bradshaw Jul 28 '17 at 04:06
  • 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? – Danny Tuppeny Dec 15 '17 at 11:04
  • 7
    Got this same issue but with Android Studio, restarting AS fixes it. – mbwasi Feb 28 '18 at 20:39
  • 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. – Bliv_Dev Mar 09 '20 at 23:30
  • restarting android studio did not work for me. `flutter pub get` worked – Aseem Mar 04 '23 at 23:42

42 Answers42

517

The issue is due to the missing packages which are being used in the project. Therefore, in order to install the same, navigate to the folder where the project is present -> cmd -> execute the following commands:

  • flutter packages get

  • flutter packages upgrade

  • restart VScode/Android studio

source: https://flutter.dev/docs/development/packages-and-plugins/using-packages

Solomon Ucko
  • 5,724
  • 3
  • 24
  • 45
JDawg
  • 8,560
  • 4
  • 25
  • 29
  • 2
    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 https://github.com/Dart-Code/Dart-Code/issues/new thanks! – Danny Tuppeny Dec 15 '17 at 11:12
  • 41
    Notice that it's now `flutter packages get ` – Zooly Mar 02 '18 at 15:35
  • 8
    I just closed the folder in the VSCode and opened again. this resolved:) – Sravan Jan 31 '19 at 11:41
  • (this worked for me) but its very odd that, we still need to do a editor restart just to connect to existing files. – Jay Sep 23 '19 at 07:34
  • 3
    This worked for me on Ubuntu (`flutter packages get` and then restarted VSCode). Thanks! – Chinnon Santos Nov 21 '19 at 13:42
  • 14
    Notice that it's now ```flutter pub get``` https://flutter.dev/docs/development/packages-and-plugins/using-packages – Mahesh Jamdade Oct 07 '20 at 04:42
  • 1
    This worked for me without any sort of restart or reload of VS Code – Stefan Aug 30 '21 at 16:29
  • I closed Android Studio project then reopened. The error was solved. – Polymath Aug 03 '22 at 01:30
  • 1
    Wow! restart VSCode is a required step. I spent so much time experimenting with `flutter pub` commands, but still got the same error. Even after being 100% sure that package exists in system cache. – Akhrameev Jul 31 '23 at 18:07
66

flutter clean

flutter packages get

flutter packages upgrade ( Optional - use if you want to upgrade packages )

Restart Android Studio or Visual Studio

Ishwar Chandra Tiwari
  • 5,099
  • 6
  • 22
  • 41
  • $ flutter clean $ flutter packages get $ flutter packages upgrade _If you are running on vscode do it in the debug mode._ – victorkolis Feb 17 '22 at 20:29
40

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.

Danny Tuppeny
  • 40,147
  • 24
  • 151
  • 275
  • 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 :) – Miiite Aug 19 '18 at 21:32
  • 4
    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? – CokoBWare Dec 06 '18 at 17:22
  • @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. – Danny Tuppeny Dec 06 '18 at 21:30
  • @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 – user603749 Apr 23 '19 at 22:53
  • @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! – Danny Tuppeny Apr 24 '19 at 15:24
24

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.

nonybrighto
  • 8,752
  • 5
  • 41
  • 55
20

Restarting visual studio code worked for me !

Komal Dewnani
  • 229
  • 2
  • 3
  • 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? – Tyler2P Dec 27 '20 at 22:58
  • Worked on Android Studio too. This belongs as an answer otherwise I wouldn't have seen it. – buckleyJohnson Jan 17 '21 at 19:34
  • Restarting the VSCode runs `flutter pub get` command which can fix the issue. – Pramod Apr 11 '21 at 15:37
17

Restart the VS Code you will see magic! If that's not work then check the indent of your dependencies in the pubspec.yaml

Yuri Haruno
  • 111
  • 1
  • 7
Mahinthan177
  • 774
  • 1
  • 9
  • 9
10

Open the project after deleting .idea folder and .dart_tool

simran agarwal
  • 401
  • 4
  • 7
9

This worked for me in Android Studio as well as VS Code. I only had to run these lines in my terminal/command prompt and problem was solved. There was no need to restart any of the IDEs again

  • flutter packages get

Optionally you also run.

  • flutter upgrade
Kennedy Owusu
  • 5,690
  • 4
  • 15
  • 20
8

-Edit:- Now this happens most of the time to me, whenever I open some old project(flutter project), it is basically because of not downloading or not having the packages to refer, for the vscode, In simple just blindly do A PUB GET or just hit CTRL/COMMAND + s in pubspec.yaml file, and you are good to go(sometimes it might take some time to get everything set(depends on your system specs.))!

-old answer starts here!

Simple thing I did after Someone said here to restart the VSCode and I did that, and now everything works fine.

For me it was because just when I was creating project I got an notification for updating my dart(or related) extension and for that I did it and boom as my project started, it just gave me around 30 errors which do scared but the simple FIX was to RESTART THE EDITOR.

An Android
  • 149
  • 1
  • 6
6

Open the command palette (ctrl+shift+p), type flutter: get packages (should autocomplete); after it resolves open the command palette again and type reload window and execute the command. This should resolve the issue as quickly as possible.

Inarus Lynx
  • 59
  • 1
  • 5
6

Close the VS Code and Run it again

goodBot
  • 231
  • 3
  • 3
5

The quickest way to get Dart-Code to reliably find your Flutter install and use it is to create a new FLUTTER_ROOT environment variable and set it to your Flutter path.

Kyle Bradshaw
  • 156
  • 2
  • 13
4

Basically

  1. Check for correct indentation of your package in dependencies
  2. if your editor supports, it automatically runs -> flutter pub get
  3. Either way -> open terminal-> flutter pub get or flutter packages get
  4. check .packages file, see if your package is present else reinstall package
  5. Most important : Restart your IDE (Visual studio or Android Studio)

Start debugging your project

Most probably , your errors will be fixed by then

Hope it works for you

4

try to run flutter clean

then restart VS Code. This did it for me.

Riccardo
  • 1,083
  • 2
  • 15
  • 25
Hez
  • 315
  • 2
  • 7
4

Restart your IDE, and run these commands:

  • flutter clean.
  • flutter pub get.
  • flutter upgrade.

if they didn't work, then check your dart SDK path.

Nour
  • 5,252
  • 3
  • 41
  • 66
Muhammad Awais
  • 141
  • 1
  • 5
2

I also had this issue. I had both VS code and Android studio installed in my system.

The error was in VS code.

When i opened the same project on Android studio, the dependency was not actually added to pubsec.yaml. I added it there and ran pub.get.

When I returned to VS Code and everything was working fine.

So, Try opening it in other editor if you have, or through NotePad.

Edit:

Opening widget_test.dart and running it should also solve your issue.

Bensal
  • 3,316
  • 1
  • 23
  • 35
1

Also happened to me while trying to run the project inside of another directory.

Worked by using the root directory of the main project

Oush
  • 3,090
  • 23
  • 22
1

Just add dependencies: http: ^0.12.0 in pubspec.yaml file please check http documentation

Ashok Chowdary
  • 101
  • 1
  • 7
1

If you execute From the terminal: "Run flutter packages get" and the error continues, check is all packages directories are listed at '.packages' file. - Sometime you have the packages, but it is not configured at this file.

1

Execute one of the following:

  • flutter upgrade

  • flutter pub get

  • flutter packages get

Dharman
  • 30,962
  • 25
  • 85
  • 135
Rashid Iqbal
  • 1,123
  • 13
  • 13
1

Warning! This package referenced a Flutter repository via the .packages file that is no longer available. The repository from which the 'flutter' tool is currently executing will be used instead.
running Flutter tool: /opt/flutter previous reference : /Users/Shared/Library/flutter This can happen if you deleted or moved your copy of the Flutter repository, or if it was on a volume that is no longer mounted or has been mounted at a different location. Please check your system path to verify that you are running the expected version (run 'flutter --version' to see which flutter is on your path).

Checking the output of the flutter packages get reveals that the reason in my case was due to moving the flutter sdk.

Tommie C.
  • 12,895
  • 5
  • 82
  • 100
1

Do not forget:

export PATH="$PATH:/home/[xxxxx]/flutter/bin

For me, it works:

flutter upgrade

flutter packages get

You can check with

flutter doctor

flutter --version
Jitesh Prajapati
  • 2,533
  • 4
  • 29
  • 51
Somplesi
  • 11
  • 2
1

Go to pubsec.yaml file and add this directly after cupertino_icons:

webview_flutter:

Then press Packages get on the top right corner and it will work fine with you.

סטנלי גרונן
  • 2,917
  • 23
  • 46
  • 68
Alex Ali
  • 1,339
  • 5
  • 23
  • 34
1

Add dependencies. for example:- import 'package:audioplayers/audio_cache.dart'; in the above package if we only use this package then it shows error but if we add dependencies in pubspec.yaml such as

dependencies:
    flutter:
      sdk: flutter
    cupertino_icons: ^0.1.2
    audioplayers: ^0.14.1

enter image description here

then click on packages get.

as you see this, I can also insert dependencies so if you insert dependencies along with your package then you are good to go.

SimplyProgrammer
  • 1,799
  • 2
  • 17
  • 28
1

If you change the property name in pubspect.yaml all your package in lib folder turn to red with the error Target of URI doesn't exist...

teteArg
  • 3,684
  • 2
  • 20
  • 18
1

I was using (goggle location picker (with much more customisation in functions and UI) ) so I copy this package(complete) and using in my app in one folder but since dart code analyser analyses one flutter project so I found that those which are referencing from inside of this package is not working then I copy only lib folder(of google location picker) in my original project folder and voila this worked for me. This solution took me a time of 3 days. I know this is not the question but it might help someone to save 3 days.

Akash Bansal
  • 139
  • 2
  • 6
1

I had this problem in Android Studio with the rflutter_alert package. After inserting the dependencies, I imported the package in my project with ctrl+space key and I have added import 'package:rflutter_alert/'; the problem is that this was not correct. The correct form was package:rflutter_alert/rflutter_alert.dart right vs wrong comparison

MariusMihai92
  • 19
  • 1
  • 6
1

i faced the same issue with the bloc package, after i restarted visual studio i installed the package again using flutter pub add bloc and it installed properly this time. Note that i tried installing it several times, this time it was a charm

1

My case was different, I pretty much tried everything suggested but with no luck :( .

In my case it didn't find several packages, for some reason it could find the folder of the packages, but not the main file of it, after not finding answer online for this, i started to look into these packages and check their dependencies and saw that I was missing one, still not sure why it found the folder but not the content of it because of a dependency, anyhow, the dependency in my case was meta, which is for annotations, many packages uses it, after adding it to all relevant module in our project, pub get... and walla no errors, phew this one was very annoying...

Ben Levi
  • 177
  • 1
  • 5
0

Don't forget to save pubspec.yaml before running "flutter pub get". Restarting Visual Studio Code is not enough. That was my solution. Sometimes when you work with intellij you forget that ...

Taiskorgon
  • 69
  • 6
0

When you usually get this error message:

Target of URI doesn't exist: 'package:foo'. Try creating the file referenced by the URI, or Try using a URI for a file that does exist.

Example:

Target of URI doesn't exist: 'package:random_string/random_string.dart'. Try creating the file referenced by the URI, or Try using a URI for a file that does exist.

import 'package:random_string/random_string.dart';

It is because a dependency is missing.

So all you have to do is find out what packages are needed by googling your package name.

enter image description here

Install the dependency:

$ flutter pub get

and add the dependency in the pubspec.yaml file:

enter image description here

live-love
  • 48,840
  • 22
  • 240
  • 204
0

I was trying use the audio players flutter package. Once I added it to pubspec.yaml and tried to import it to main.dart, I got the same error.

I tried to restart my IDE but that didn't help so i tried running flutter packages pub cache repair and it worked.

Joseph Jacob
  • 163
  • 1
  • 7
  • 1
    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. – Mustafa Apr 22 '20 at 02:24
0

I was following the Flutter get started tutorial and VSCode could not find the package for english_words, despite it showing in pubspeck.lock and .packages. Hot reload didn't work.

To make it work, I stopped the instance and re-run it - so I didn't have to restart VSCode.

vk1011
  • 7,011
  • 6
  • 26
  • 42
0

I was facing with the same issue and tried everything restarting, packages get, repair everything, But none of these solution worked. The only thing which worked was to set the encoding of the packages file giving error to UTF-8.

PS: I faced this on Android studio

Hope this would help someone.

Ronak Jain
  • 159
  • 1
  • 7
0

I didn't think this was possible: I had to delete flutter folder and reinstall it from scratch!

marcolav
  • 405
  • 1
  • 6
  • 17
0

Instead of just restarting vsCode you can just hit ⌘+r or CTRL+R in Windows, this will reload your VSCode.

Benjamin
  • 11
  • 1
0

Target of URI doesn't exist: 'package:provider/provider.dart'.

I had this show up when trying to import - import 'package:provider/provider.dart';

I was using Android Studio

I closed Android studio and restarted it and everything was fine

0

I had the same issue, and after that I ran the following code:

  • flutter pub add firebase_auth

  • flutter pub add firebase_core

Yash Patel
  • 39
  • 2
0
  • run flutter pub add firebase_core

Most Important - Goto your flutter installation directory and check if Flutter_core is there, if it's not there, run flutter pub add firebase_core

If it's not fixed still try these as mentioned everywhere

  • flutter pub get
  • Restart Visual Studio Code or Android Studio
Matt
  • 464
  • 4
  • 13
0

Make sure you run this code flutter pub add firebase_core

SaifAlmajd
  • 34
  • 4
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 27 '22 at 11:51
0

enter image description here

go to settings / language & framework / dart and enable project 'YOUR_PROJECT' then support it

Jakob Liskow
  • 1,323
  • 14
  • 22
0

flutter packages get

flutter packages upgrade

restart VScode/Android studio

If you used the flutter package get command and the error still persists, you can reload the VS code with the Developer: Reload Window command. Just type this after pressing Ctrl+Shift+P (Cmd+Shift+P for Mac users). It will clear the error. It's like refreshing VS Code. this will work for you.