James
James

Reputation: 4052

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

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?

Upvotes: 306

Views: 460429

Answers (30)

Ishwar
Ishwar

Reputation: 6141

Run these commands: -

flutter clean

flutter packages get

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

Then restart Android Studio or Visual Studio.

Upvotes: 85

I have screwed my IDE totally.

Solution for me was to run two commands:

flutter downgrade

and then

flutter upgrade

Upvotes: 0

swetha sasanapuri
swetha sasanapuri

Reputation: 365

  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

Upvotes: 5

An Android
An Android

Reputation: 159

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 VS Code, In simple just blindly do a PUB GET or just hit CTRL/CMD + 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 VS Code 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.

Upvotes: 9

Ronak Jain
Ronak Jain

Reputation: 304

I was facing with the same issue on Android studio 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.

Upvotes: 0

SEZER OĞRAŞ
SEZER OĞRAŞ

Reputation: 59

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.

Upvotes: 0

Muhammad Awais
Muhammad Awais

Reputation: 169

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.

Upvotes: 7

idriss askour
idriss askour

Reputation: 1

enter image description here

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

Upvotes: 0

JDawg
JDawg

Reputation: 9500

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

Upvotes: 580

Ben Levi
Ben Levi

Reputation: 187

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...

Upvotes: 1

Matt
Matt

Reputation: 474

  • 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

Upvotes: 0

Yash Patel
Yash Patel

Reputation: 39

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

  • flutter pub add firebase_auth

  • flutter pub add firebase_core

Upvotes: 0

Mahinthan177
Mahinthan177

Reputation: 846

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

Upvotes: 18

Jason Braithwaite
Jason Braithwaite

Reputation: 119

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

Upvotes: 0

Victor Ngalika
Victor Ngalika

Reputation: 21

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

Upvotes: 1

Benjamin
Benjamin

Reputation: 11

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

Upvotes: 0

simran agarwal
simran agarwal

Reputation: 401

Open the project after deleting .idea folder and .dart_tool

Upvotes: 10

MariusMihai92
MariusMihai92

Reputation: 29

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

Upvotes: 1

Bensal
Bensal

Reputation: 4110

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.

Upvotes: 2

Hez
Hez

Reputation: 375

try to run flutter clean

then restart VS Code. This did it for me.

Upvotes: 4

Akash Bansal
Akash Bansal

Reputation: 138

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.

Upvotes: 1

marcolav
marcolav

Reputation: 485

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

Upvotes: 0

teteArg
teteArg

Reputation: 4004

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...

Upvotes: 1

Kennedy Owusu
Kennedy Owusu

Reputation: 6070

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

Upvotes: 10

vk1011
vk1011

Reputation: 7179

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.

Upvotes: 0

Joseph Jacob
Joseph Jacob

Reputation: 173

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.

Upvotes: 1

live-love
live-love

Reputation: 52366

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

Upvotes: -1

ashbab khan
ashbab khan

Reputation: 11

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.

Upvotes: 1

goodBot
goodBot

Reputation: 241

Close the VS Code and Run it again

Upvotes: 7

Taiskorgon
Taiskorgon

Reputation: 69

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 ...

Upvotes: 0

Related Questions