Flutter: How to resolve Error importing package:http/http.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

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

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.


You need to import like this:

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

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