29

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)

3 Answers3

56

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.

Paulo Belo
  • 3,759
  • 1
  • 22
  • 20
smorgan
  • 20,228
  • 3
  • 47
  • 55
  • 1
    I can confirm that following the accepted solution did NOT add the entitlement to release, even though I had selected "All" in the capabilities. I was skeptical but this should be the accepted answer IMO. – Ryan Knell Jun 08 '20 at 00:51
  • After editing these two files - do a full stop/start cycle in VSCode debugger for changes to take effect. – Eugene Kulabuhov Feb 16 '23 at 23:39
  • that's work for me in debug, have not test on release – Ammar Apr 09 '23 at 10:53
32

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.

  • 4
    You should *not* use the Capabilities UI for the macOS Flutter Runner. See https://github.com/google/flutter-desktop-embedding/blob/master/macOS-Security.md. It is very likely that you only enabled outgoing connections for either Debug+Profile or Release, but not both. – smorgan Sep 08 '19 at 21:04
  • 1
    @smorgan Thanks for the clarification. I'm surprised the `com.apple.security.network.client` was not enabled by default. However, the files in question are: `/macos/Runner-DebugProfile.entitlements` and `/macos/Runner-Release.entitlements`. – kosiara - Bartosz Kosarzycki Sep 09 '19 at 16:20
  • Could you elaborate on why you expected it to be enabled by default? Internet access isn't enabled by default in Flutter for (release) Android either (https://flutter.dev/docs/deployment/android#reviewing-the-app-manifest), so this is consistent with how Flutter currently works (iOS doesn't have such a setting, so there's no way to compare there). Similarly, a default macOS project template in Xcode gives you a sandboxed application without internet access enabled. Is there a reason you expect Flutter for macOS to be less secure by default than Flutter for mobile or native macOS projects? – smorgan Sep 09 '19 at 18:23
  • How to enable internet connection for flutter desktop app in IntelliJ? – K.Os Apr 13 '20 at 20:57
  • @K.Os See the link in my first comment. I've also added it as a separate answer now. – smorgan Apr 14 '20 at 05:17
  • 1
    @kosiara-BartoszKosarzycki Please consider removing or editing your answer; people who read your answer but not the comments on it are getting incorrect information on how they should add internet capabilities to a Flutter application. – smorgan Apr 14 '20 at 05:19
  • Although I did everything completely, I cannot connect to the internet in the release version. – Ufuk Zimmerman Apr 06 '21 at 18:04
2

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
Rahul sharma
  • 1,492
  • 12
  • 26