Flutter - http.get fails on macos build target: Connection failed

I started porting my Flutter app to macos Desktop. The UI started fine. However, as soon as the app makes a network call, it fails instantly with Connection failed (OS Error: Operation not permitted).

Running a one-liner:

final response = await http.get('https://jsonplaceholder.typicode.com/posts/1');

fails with:

Unhandled Exception: SocketException: Connection failed (OS Error: 
    Operation not permitted, errno = 1),
         address = jsonplaceholder.typicode.com, port = 443
#0      IOClient.send (package:http/src/io_client.dart:33:23)

The macos build target comes from Google's sample here.

Flutter (Channel master, v1.9.8-pre.108)

Upvotes: 35

Views: 16352

Answers (3)

smorgan
smorgan

Reputation: 21599

Per my comment on the other answer, you should not use the Xcode capabilities UI for this. It will not correctly handle the two separate entitlement files in a Flutter project.

You need to add:

<key>com.apple.security.network.client</key>
<true/>

to macos/Runner/DebugProfile.entitlements and macos/Runner/Release.entitlements.

This is documented here and here.

Upvotes: 70

Rahul sharma
Rahul sharma

Reputation: 1574

If you are getting this error Only in Android release then Internet permission must be missing from main manifest. You just need to add Internet permission in Manifest file.

Just add this permission in manifest <uses-permission android:name="android.permission.INTERNET"/>

There are three manifest file available in Android folder

app/src/debug
app/src/main
app/src/profile

Upvotes: 1

Your macOS XCode project lacks Internet permission called "Outgoing Connections (Client)".

Open your macos xcode project - [root]/macos/Runner.xcworkspace

Click "Runner" in Project navigator - general settings will show up. Select "Capabilities" from tabbar and tick option "Outgoing Connections (Client)".

enter image description here

Rebuild your application inside XCode and launch the project.

Upvotes: 36

Related Questions