54
votes

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?

12
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 - griffins

12 Answers

99
votes

It is clear way to add http to flutter

  1. Add this to your package's pubspec.yaml file:
dependencies:
  http: ^0.12.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

25
votes

Did you add it to pubspec?

dependencies:
  flutter:
    sdk: flutter

  http: 0.12.0
11
votes

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

5
votes

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.

4
votes

Add dependencies to pubspec like this.

dependencies:
  http: ^0.12.0

**** update packages get.

**** update dependencies.

3
votes

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.

2
votes
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
2
votes

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

2
votes

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.

1
votes

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

1
votes

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.

0
votes

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.