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

I've just setup my Macbook for flutter development,

So I downloaded flutter sdk, and placed it in my Documents. After, I setup 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 setup 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 its 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?

This question is related to android dart visual-studio-code flutter

The answer is


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.


Sometimes, this issue is resolved simply by running flutter pub get once again...

packages get to make sure that all packages are considered...

as when moving the project from one computer to another, this may happen, that the packages are not taken into consideration, so flutter pub get and there you go !!!


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

Worked by using the root directory of the main project


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


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.


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.


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.


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.


flutter clean

flutter packages get

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

Restart Android Studio or Visual Studio


Close the VS Code and Run it again


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


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.


Restarting visual studio code worked for me !


Restarting Visual Studio Code after

flutter pub get

resolved the error messages for me.

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


try to run flutter clean

then restart VS Code. This did it for me.


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


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


Restart the VS Code you will see magic! If that's not work check the indent in the pubsepec.yaml


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.


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


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.


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


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.


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


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

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.


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.


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.


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

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.


Execute one of the following:

  • flutter upgrade

  • flutter pub get

  • flutter packages get


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.


Open the project after deleting .idea folder and .dart_tool


Examples related to android

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How to implement a simple scenario the OO way My eclipse won't open, i download the bundle pack it keeps saying error log getting " (1) no such column: _id10 " error java doesn't run if structure inside of onclick listener Cannot retrieve string(s) from preferences (settings) strange error in my Animation Drawable how to put image in a bundle and pass it to another activity FragmentActivity to Fragment A failure occurred while executing com.android.build.gradle.internal.tasks

Examples related to dart

How to integrate Dart into a Rails app Flutter Countdown Timer How to make an AlertDialog in Flutter? Set the space between Elements in Row Flutter Flutter: RenderBox was not laid out Space between Column's children in Flutter How to change status bar color in Flutter? How can I add shadow to the widget in flutter? Flutter - The method was called on null Flutter- wrapping text

Examples related to visual-studio-code

TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received type undefined raised when starting react app Visual Studio Code PHP Intelephense Keep Showing Not Necessary Error Cannot edit in read-only editor VS Code How to setup virtual environment for Python in VS Code? Pylint "unresolved import" error in Visual Studio Code Why do I keep getting Delete 'cr' [prettier/prettier]? How to set up devices for VS Code for a Flutter emulator VSCode single to double quote automatic replace js 'types' can only be used in a .ts file - Visual Studio Code using @ts-check How can I clear the terminal in Visual Studio Code?

Examples related to flutter

Flutter Countdown Timer How to make an AlertDialog in Flutter? FlutterError: Unable to load asset Set the space between Elements in Row Flutter Flutter: RenderBox was not laid out Space between Column's children in Flutter How to change status bar color in Flutter? How can I add shadow to the widget in flutter? Flutter - The method was called on null Flutter- wrapping text