Sub topic 2
Allowing Network Access on Each Platform
This chapter assumes you know Dart classes, null safety, Future, async and await, basic Flutter widgets, and how to edit pubspec.yaml. The http package sends the same Dart request on every platform, but each operating system applies its own network rules.
The platform differences
A Flutter app may run on Android, iOS, web, macOS, Windows, or Linux. HTTPS usually works without special exceptions. Plain HTTP, local network access, and sandbox permissions need extra attention.
| Platform | Main configuration | Common restriction |
|---|---|---|
| Android | Declare the Internet permission | Cleartext HTTP is blocked by default on recent Android versions |
| iOS | Use HTTPS or configure App Transport Security | Non-HTTPS requests are blocked by default |
| Web | Configure the API server | Browser CORS rules apply |
| macOS | Enable the network client entitlement | App Sandbox can block outgoing connections |
| Windows and Linux | Usually no app manifest change | Firewalls or operating-system policies may block traffic |
Android
Open android/app/src/main/AndroidManifest.xml. Add the permission as a direct child of the <manifest> element, before <application>:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.my_app">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:label="my_app"
android:name="${applicationName}"
android:icon="@mipmap/ic_launcher">
<!-- Flutter's generated application configuration remains here. -->
</application>
</manifest>The permission allows the app to open network connections. It does not make an insecure server safe.
Android 9 and later block cleartext URLs such as http://api.example.com by default. Prefer an HTTPS endpoint. If you control a development server that only supports HTTP, configure a narrow development exception rather than enabling cleartext traffic for the whole app.
An Android emulator cannot reach your computer through localhost. Inside the standard Android emulator, use 10.0.2.2 for the host computer. A physical device normally needs your computer's local network address, and both devices must be on the same network.
iOS
iOS uses App Transport Security, commonly called ATS. ATS requires secure HTTPS connections. You normally do not need to request a runtime permission for Internet access.
For local development, use HTTPS when possible. If an internal server cannot provide HTTPS, edit ios/Runner/Info.plist with a narrowly scoped ATS exception. Avoid disabling ATS globally because that permits insecure connections to unintended hosts.
The iOS simulator can often use localhost to reach services on the development computer. A physical iPhone may need the computer's LAN address instead.
Web, macOS, Windows, and Linux
A Flutter web app runs inside a browser. The browser sends an Origin header, and the API server must return suitable CORS headers. Adding a Flutter setting cannot bypass CORS. Configure the server to allow the specific web origins your app uses.
macOS applications built with the App Sandbox need the network client entitlement. In macos/Runner/DebugProfile.entitlements and, when required, macos/Runner/Release.entitlements, make sure this entry exists:
<key>com.apple.security.network.client</key>
<true/>Windows and Linux generally require no Flutter-specific network permission. A firewall, proxy, VPN, or corporate security policy can still reject the connection.
Check configuration systematically
When a request fails, check these items in order:
- Confirm the URL uses the correct scheme, host, and port.
- Test the same URL from the target device or browser.
- Check the platform permission or entitlement.
- Replace
localhostwith the correct emulator or device address. - Check HTTPS certificates and ATS or cleartext restrictions.
- For web, inspect the browser console for a CORS error.
- Check whether a firewall or proxy blocks the port.
Exercise
Your Android app requests http://localhost:8080/posts and fails on an emulator. Name two likely causes and their fixes.
Show answer
Android cleartext traffic may be blocked, so use HTTPS or a narrow development exception. Also, localhost points to the emulator itself, so use 10.0.2.2:8080 for a server running on the host computer.