Flutter: How to resolve Error importing package:http/http.dart

DartFlutter

Dart Problem Overview


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.

Dart Solutions


Solution 1 - Dart

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

Solution 2 - Dart

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

Solution 3 - Dart

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.

Solution 4 - Dart

You need to import like this:

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

Solution 5 - Dart

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

Solution 6 - Dart

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 .

Solution 7 - Dart

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.

Solution 8 - Dart

this worked for me:

dependencies:
  flutter:
    sdk: flutter

  cupertino_icons: ^0.1.2
  http: any

then:

flutter doctor -v

Finally:

flutter packages get

Solution 9 - Dart

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;

Solution 10 - Dart

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

Solution 11 - Dart

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

Solution 12 - Dart

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

Solution 13 - Dart

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;

Solution 14 - Dart

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.

Solution 15 - Dart

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

Solution 16 - Dart

cupertino_icons: ^0.1.2

http: any

Solution 17 - Dart

http: have to be align with the other dependencies.

Solution 18 - Dart

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';

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAthar AnsariView Question on Stackoverflow
Solution 1 - DartdiegoveloperView Answer on Stackoverflow
Solution 2 - DartAkshat TamrakarView Answer on Stackoverflow
Solution 3 - DartJustin BuistView Answer on Stackoverflow
Solution 4 - DartartoisView Answer on Stackoverflow
Solution 5 - DartSamView Answer on Stackoverflow
Solution 6 - DartMuhammad AliView Answer on Stackoverflow
Solution 7 - DartMastersamView Answer on Stackoverflow
Solution 8 - DartDiego Santa Cruz MendezúView Answer on Stackoverflow
Solution 9 - DartBauroziqView Answer on Stackoverflow
Solution 10 - DartSarthak RavalView Answer on Stackoverflow
Solution 11 - DartMr vdView Answer on Stackoverflow
Solution 12 - DartKunjal Sarma View Answer on Stackoverflow
Solution 13 - DartSandeep PareekView Answer on Stackoverflow
Solution 14 - DartDulya PereraView Answer on Stackoverflow
Solution 15 - DartMohammadSooriView Answer on Stackoverflow
Solution 16 - DartDinuwan KalubowilaView Answer on Stackoverflow
Solution 17 - DartFrankely Hernandez ReynaView Answer on Stackoverflow
Solution 18 - DartUsmanView Answer on Stackoverflow