Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter Websockets MacOS: The same code works in debug mode but not in release mode: "(OS Error: nodename nor servname provided..)"

Summary

I have a program using WebSockets in Flutter, the program is fully functional in debug mode, but running the same exact code in release mode causes an error.

Context

The Flutter documentation teaches us how to work with WebSockets.

Let's use consider the official code example to work with WebSockets (I've modified it a bit to display the error in a Text Widget):

import 'package:flutter/material.dart';
import 'package:web_socket_channel/web_socket_channel.dart';

void main() => runApp(const MyApp());

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    const title = 'WebSocket Demo';
    return const MaterialApp(
      title: title,
      home: MyHomePage(
        title: title,
      ),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({
    super.key,
    required this.title,
  });

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  final TextEditingController _controller = TextEditingController();
  final _channel = WebSocketChannel.connect(
    Uri.parse('wss://echo.websocket.events'),
  );

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Padding(
        padding: const EdgeInsets.all(20.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Form(
              child: TextFormField(
                controller: _controller,
                decoration: const InputDecoration(labelText: 'Send a message'),
              ),
            ),
            const SizedBox(height: 24),
            StreamBuilder(
              stream: _channel.stream,
              builder: (context, snapshot) {
                if (snapshot.hasData) {
                  return Text(snapshot.data.toString());
                } else if (snapshot.hasError) {
                  return Text(snapshot.error.toString());
                } else {
                  return const Text('listening...');
                }
              },
            )
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _sendMessage,
        tooltip: 'Send message',
        child: const Icon(Icons.send),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }

  void _sendMessage() {
    if (_controller.text.isNotEmpty) {
      _channel.sink.add(_controller.text);
    }
  }

  @override
  void dispose() {
    _channel.sink.close();
    _controller.dispose();
    super.dispose();
  }
}

The problem

If you run the above code snippet on MacOS in debug mode, it will work correctly. However, If you build the app using flutter build macos, you'll get this error:

SocketException: Failed host lookup: ‘...’ (OS Error: nodename nor servname provided, or not known, errno = 8)

enter image description here

What I tried

  1. SocketException: Connection failed (OS Error: Operation not permitted, errno = 1) with flutter app on macOS which said to add this <key>com.apple.security.network.client</key> <true/>

  2. SocketException: Failed host lookup: ‘...com’ (OS Error: nodename nor servname provided, or not known, errno = 8)

  3. ssh: Could not resolve hostname [hostname]: nodename nor servname provided, or not known

  4. Turned off my FireWall.

  5. Made sure there's an active Internet connection.

Question

Why does the above snippet work in Debug mode, but not in release mode when running flutter build macos?

Note

Running

flutter doctor -v

says "no issues found".

like image 326
MendelG Avatar asked Nov 02 '25 09:11

MendelG


1 Answers

You said it works on debug mode, but not on release mode, so I suspect you have already added the entitlements to the debug profile, but you probably forgot to add it to the release profile.

Under macos > Runner directory, find both "DebugProfile.entitlements" and "Release.entitlements" files:

folder structure screenshot

Add this section to both files:

<key>com.apple.security.network.client</key>
<true/>
like image 195
user1032613 Avatar answered Nov 03 '25 23:11

user1032613