Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Flutter and app store releases

Flutter cannot import package:http/http.dart

ErrorError importing package:http/http.dart

Short answer

The import fails when the http package is not in pubspec.yaml or was never installed. Run flutter pub add http from the app root, then import it as package:http/http.dart with the prefix http. If requests fail on Android or macOS after that, the INTERNET permission or the network client entitlement is missing.

  • Applies to Flutter 3.35, Dart 3.9
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Check whether http is a dependency of this app and was resolved.

    grep -n "http" pubspec.yaml pubspec.lock
  2. 2Match what you see, then jump to its fix

    1. 1No http entry under dependencies in pubspec.yamlThe http package was never addedFix
    2. 2Errors on the calls such as http.get after the importThe import line or the prefix is wrongFix
    3. 3Code compiles, but no data arrives on Android or macOSThe import works but requests fail on Android or macOSFix

What does the error look like?

  • Error importing package:http/http.dart
  • import 'package:http/http.dart' as http;

Cause 1 of 3

The http package was never added

1Check

The import only works for packages listed in the app's own pubspec.yaml. Check the file from the app's root folder.

sed -n '/^dependencies:/,/^dev_dependencies:/p' pubspec.yaml

2Fix

Add it with the command, which edits pubspec.yaml and installs it.

flutter pub add http

3Verify

pubspec.lock now lists http, and the analyzer accepts the import.

flutter analyze

Confirmed by docs.flutter.dev

Did this fix it?

Cause 2 of 3

The import line or the prefix is wrong

1Check

The Flutter networking recipe imports the package with the prefix http, and advises against dart:io or dart:html for requests because they depend on the platform.

grep -rn "import .*http" lib/

2Fix

Use the documented import line, and call the functions through the prefix.

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

final response = await http.get(Uri.parse('https://example.com/api'));

3Verify

The file analyzes without errors.

flutter analyze

Confirmed by docs.flutter.dev

Did this fix it?

Cause 3 of 3

The import works but requests fail on Android or macOS

1Check

Android needs the INTERNET permission in AndroidManifest.xml. A macOS app needs the network client entitlement in both entitlements files.

grep -rn "INTERNET" android/app/src/main/AndroidManifest.xml; grep -n "network.client" macos/Runner/*.entitlements

2Fix

Add the permission on Android. On macOS add the entitlement to both DebugProfile.entitlements and Release.entitlements.

<!-- android/app/src/main/AndroidManifest.xml -->
<uses-permission android:name="android.permission.INTERNET" />

<!-- macos/Runner/DebugProfile.entitlements and Release.entitlements -->
<key>com.apple.security.network.client</key>
<true/>

3Verify

Rebuild the app fully, then repeat the request.

flutter run

Confirmed by docs.flutter.dev

Did this fix it?

How do I stop it coming back?

  • Add packages with flutter pub add so pubspec.yaml and the lockfile change together.
  • Add the INTERNET permission and the macOS entitlement when you create a networking feature, not when it fails.
  • Run flutter analyze before every commit.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.