55

I am very new to Flutter, and stuck at the following error:

package:http/http.dart That library is in a package that is not known.

Maybe you forgot to mention it in your pubspec.yaml file?

Please help in resolving this error.

Philippe Fanaro
  • 6,148
  • 6
  • 38
  • 76
Athar Ansari
  • 559
  • 1
  • 4
  • 4
  • 2
    that package is part of flutter and should not show an error if you import it. To help you, you should post your relevant parts of the pubspec.yaml and .dart-File – Christian Nov 24 '18 at 22:37
  • The package is not a part of flutter but it is distributed via pub. – Jonah Williams Nov 25 '18 at 00:02

19 Answers19

99

Go to your pubspec.yaml file , and add the http dependency:

dependencies:
  flutter:
    sdk: flutter

  cupertino_icons: ^0.1.2
  http: any

Remember, the indentation must be the same as 'flutter' or 'cupertino_icons'

And don't forget to run this command in your terminal :

flutter packages get
diegoveloper
  • 93,875
  • 20
  • 236
  • 194
  • 7
    And don't for get to `pub get` or `flutter packages get` after saving. – Nate Bosch Nov 25 '18 at 04:21
  • [https://stackoverflow.com/questions/61037356/what-is-the-difference-between-flutter-packages-get-and-flutter-pub-get](apparently) `flutter pub` is just an alias to `flutter packages` – pfincent Jun 01 '22 at 13:29
21

I had the same problem. You just have to add latest http dependencies in pubspec.yaml

Here is the full code,

  dependencies:
      flutter:
        sdk: flutter

      http: ^0.12.0+1

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

Don't forget to change verson no. of http To check latest version of http click here

If you are running app directly from command line then you could also do:

with Flutter:

$ flutter packages get

with pub:

$ pub get
ray
  • 5,454
  • 1
  • 18
  • 40
Akshat Tamrakar
  • 2,193
  • 2
  • 13
  • 21
13

I've found that, when in VS Code, you might need to restart the editor to get it to see the new http module that 'pub get' pulled in. You can have your .dart code and pubspec.yaml exactly right but you still get the compiler/lint error.

Justin Buist
  • 142
  • 2
  • 6
  • this worked for me after installing the package using `flutter pub add http` and imported it like this: `import 'package:http/http.dart' as http;` i still get compiler error, closed my folder and open it up again in Vs Code, error's gone and editor shows warning for unused import only. – Dids Apr 23 '22 at 09:17
10

You need to import like this:

import 'package:http/http.dart' as http;
artois
  • 485
  • 1
  • 6
  • 13
5

add below to pubspec.ymel file

dependencies:
  flutter:
    sdk: flutter

  cupertino_icons: ^0.1.2
  http: ^0.12.0+1

use flutter pub to find the latest version

call

flutter packages get
Jared Burrows
  • 54,294
  • 25
  • 151
  • 185
Sam
  • 6,215
  • 9
  • 71
  • 90
5

1.Install http package

flutter pub add http

2.check pubspec.yaml file

dependencies: 
  http: ^0.13.4

3.Import in file

import 'package:http/http.dart';

Reference

Sarthak Raval
  • 1,001
  • 1
  • 10
  • 23
4

just got to pubspec.yaml file and tweak some changes in file

dependencies:
  flutter:
    sdk: flutter

  cupertino_icons: ^0.1.2

 http:   

under dependencies section just add http like in code description .

flutter will automatically resolve it and will add updated version of http .

Muhammad Ali
  • 179
  • 1
  • 10
3

You have to add the http dependency in the pubspec.yaml file as shown below:

dependencies:
  flutter:
    sdk: flutter

  cupertino_icons: ^0.1.2
  http: ^0.12.0+2

Please take note of the indentation. After doing the above, run the below command in your terminal:

flutter packages get

As at the time of writing this, the latest http version is 0.12.0+2. You can learn more by visiting this link.

Codefarmer
  • 684
  • 4
  • 8
2

this worked for me:

dependencies:
  flutter:
    sdk: flutter

  cupertino_icons: ^0.1.2
  http: any

then:

flutter doctor -v

Finally:

flutter packages get
2

1. Add this to your package's pubspec.yaml file:

dependencies: http: ^0.12.1

2. install it :

$ flutter pub get

3. Now in your Dart code, you can use:

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

Bauroziq
  • 951
  • 9
  • 14
0

Add below to pubspec.yaml file

  dependencies:
  http: ^0.12.0+1
 

Or Just add http

dependencies:
http:

flutter will automatically used latest version of http .

Then use pub get

Mr vd
  • 878
  • 1
  • 10
  • 19
0

version: 1.0.0+1

environment: sdk: ">=2.7.0 <3.0.0"

dependencies: flutter: sdk: flutter

cupertino_icons: ^1.0.0 http: ^0.12.2

dev_dependencies: flutter_test: sdk: flutter

flutter:

uses-material-design: true assets: - images/

try to update the sdk versions and then pub get or flutter pub get to get the packages get install

0

just add in pubspec.yaml

http: ^0.12.0+2

like as

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

and run in terminal

flutter pub get

finally add in class

import 'package:http/http.dart' as http;
Sandeep Pareek
  • 1,636
  • 19
  • 21
0

When I got this issue, I just close the project from Android Studio (File -> Close Project). And then open it again.

That works for me.

Simply just Restart the project.

But Make sure to add the certain dependency to the pubspec.yaml file and do pub get.

Dulya Perera
  • 147
  • 2
  • 11
0

-Just run this below command in your project directory to install http module

flutter pub add http

  • 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 22 '22 at 19:03
-1

You first find 'dependencies:' in pubspec.yaml then under 'dependencies:' type http: ^0.12.0+2 and then press ctrl + s to run get packages.

sample:

dependencies:
  http: ^0.12.0+2
  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
MohammadSoori
  • 2,120
  • 1
  • 15
  • 17
-1

cupertino_icons: ^0.1.2

http: any

-2

http: have to be align with the other dependencies.

-2

I had the same issue although I had the latest http package in Pubspec.yaml file. This error was still in there I did these 2 steps and error was resolved.

  1. Remove import 'dart:_http'; if it is there
  2. Add import 'dart:convert';
Usman
  • 2,547
  • 31
  • 35