70

normally the package should be imported like this:

import 'package:http/http.dart' as http;

but now I get this error:

[dart] Target of URI doesn't exist: 'package:http/http.dart'. [uri_does_not_exist]

did it somehow changed in the new updates of Flutter? if so, how can I perform a get request now?

1
  • 1
    if on vs code this might happen if your flutter project is in a sub-folder of the folder open in Code.and as you can see in comments restarting code or android studio solves the problem Commented Aug 17, 2019 at 17:36

15 Answers 15

125

It is clear way to add http to flutter

  1. Add this to your package's pubspec.yaml file:
dependencies:
  http: ^1.0.0 // latest one might change
  1. Install it You can install packages from the command line:

with pub:

$ pub get

with Flutter:

$ flutter packages get

  1. Import it Now in your Dart code, you can use:

import 'package:http/http.dart';

If you have done these 3 steps restart your code editor

Sign up to request clarification or add additional context in comments.

5 Comments

The part where you restart your code editor seems important. That is what worked for me.
Restarting Android Studio fixed the problem for me.
I don't know somehow ever after re-start it is not working for me.
That was also the case with me using Visual Studio Code. Add package to dependencies, restart studio then the problem went way.
This version might not resolve in modern versions of flutter. You can find the latest version number at pub.dev/packages/http/install
28

Did you add it to pubspec?

dependencies:
  flutter:
    sdk: flutter

  http: 0.12.0

3 Comments

I up voted this because the example showed me how to add the dependency with proper indentation. Or at least I wasn't successful until I indented this way.
Thank you very much.. This was my problem. You helpt me a lot
awesome solution
13

In Android Studio import like this

  1. Go to pubspec.yaml
  2. Add dependency http: ^0.12.0+2
  3. Click on Packages get at the top

enter image description here

Few important things:

1) Follow the proper indentation while adding the dependency.

Correct:

dependencies:
  flutter:
    sdk: flutter
  http: ^0.12.0+2

Incorrect:

dependencies:
  flutter:
    sdk: flutter
    http: ^0.12.0+2

If you add dependency like this and hit Packages get then,
You will receive the error:

Error on line 21, column 5 of pubspec.yaml: A dependency may only have one source. sdk: flutter

2) Finding package and latest dependency.

You can find the latest HTTP package Here
All the Dart packages are published on Pubsite where you can find and see trending packages. enter image description here

Comments

5

Your code looks fine. However after reading the comments below Andrey's answer, you must also make sure that you run flutter packages get or pub get after adding the http dependency to your pubspec.yaml.

3 Comments

Did that too. that's not the issue.
Weird. Can you provide your pubspec.yaml here?
solved the issue. Restarted the IDE and the package worked fine. very weird.
5

Add:

dependencies:
flutter:
sdk: flutter
http: ^0.12.0

to pubspec.yaml, update Packages.get and Packages.upgrade. If still not working restart the IDE.

Comments

5

The answers here mention the package version. If you don't know the package version, but know the package name (as in your case), simply do:

flutter pub add <package_name>

Which, in your case translates to flutter pub add http.

This always fetches the latest package that you can use with your project.

Comments

4

Add dependencies to pubspec like this.

dependencies:
  http: ^0.12.0

**** update packages get.

**** update dependencies.

Comments

4
dependencies:
  flutter:
    sdk: flutter

  # The following adds the Cupertino Icons font to your application.
  # Use with the CupertinoIcons class for iOS style icons.
  cupertino_icons: ^0.1.2
  http: ^0.12.0

Comments

4

First of all, simply restart your code editor The caret ^ is not mandatory. You could add to your pubspec.yaml

dependencies:
  http: 0.12.0+2

flutter packages get is called automatically when you save the file if you are using VSCode or Android studio.

The problem for me was solved by restarting VSCode.

Comments

3

Go to pubspec.yaml file . then add

http: any

below dependencies:

  dependencies:
    flutter:
    sdk: flutter

    cupertino_icons: ^0.1.2
    http: any

then click Packages get

1 Comment

Works For me, simple.
2

try adding the http dependencies before the flutter dependencies and click Packages get

dependencies:
  http: ^0.12.0
  flutter:
    sdk: flutter

If you put after flutter that will make an error. Hope this helps

Comments

1

Clear the contents from .pub-cache folder located inside your flutter installation directory and then create a blank flutter project in android studio/ VS code and add http dependencies and click on "Packages get".

Notice that "http" package should be generated inside .pub-cache\hosted\pub.dartlang.org folder and then in your current project open pubspec.yaml file and click "Packages get", this trick should now resolve your "http" dependencies.

Note: Make sure your network is not blocking the downloads from Pub site and the current project is closed while adding "http" dependencies in another project.

Comments

0

After doing all the steps provided by @Bawantha if the issue still persists then try the following steps this worked for me.

  1. Restart your android studio.
  2. Go to flutter Inspector toolbar
  3. Click on refresh widget info button.enter image description here

Note: Make sure you have admin rights to do this.

Comments

0

All existing answers say to add the http dependency to pubspec.yaml,
however that is no longer required.

I was getting this error for a different reason;
I was getting this error in a library file, which had the line, part of my_library.

To fix it I needed to move the import 'package:http/http.dart' as http; line from my part of my_library file into my main library my_library file.

Hopefully this is helpful to someone else.

Comments

0

enter image description here

dependencies: flutter: sdk: flutter cupertino_icons: ^1.0.2 http: ^0.13.5

use this, you shoud not use space infront of http: ^0.13.5, use tab button for get alignment, and click pub_get, you should connect to internet first, good luck!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.