Summary
我有一个在 Flutter 中使用 WebSockets 的程序,该程序在debug模式,但运行相同exact代码在release模式会导致错误。
Context
Flutter 文档告诉我们如何使用 WebSocket https://docs.flutter.dev/cookbook/networking/web-sockets.
让我们考虑使用official使用 WebSockets 的代码示例(我对其进行了一些修改以在Text
小部件):
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();
}
}
问题
如果您在 MacOS 上以调试模式运行上述代码片段,它将正常工作。但是,如果您使用以下方式构建应用程序flutter build macos
,你会得到这个错误:
SocketException: Failed host lookup: ‘...’ (OS Error: nodename nor servname provided, or not known, errno = 8)
我尝试过的
-
SocketException: Connection failed (OS Error: Operation not permitted, errno = 1) with flutter app on macOS https://stackoverflow.com/questions/65458903/socketexception-connection-failed-os-error-operation-not-permitted-errno-1?rq=1
which said to add this
<key>com.apple.security.network.client</key> <true/>
-
SocketException: Failed host lookup: ‘...com’ (OS Error: nodename nor servname provided, or not known, errno = 8) https://stackoverflow.com/questions/66063913/socketexception-failed-host-lookup-com-os-error-nodename-nor-servname-p
-
ssh: Could not resolve hostname [hostname]: nodename nor servname provided, or not known https://stackoverflow.com/questions/20252294/ssh-could-not-resolve-hostname-hostname-nodename-nor-servname-provided-or-n
-
关闭我的防火墙。
-
确保有有效的互联网连接。
Question
为什么上面的代码片段在 Debug 模式下可以工作,但在运行时却不能在 Release 模式下工作flutter build macos
?
Note
Running
flutter doctor -v
说“没有发现问题”。