问题:SocketException: Failed to create server socket (OS Error: Permission denied, errno = 13)。
在安卓手机上的表现就是白屏,无法显示页面。
I/flutter (25532): Observatory server failed to start after 1 tries I/flutter (25532): Observatory server failed to start after 2 tries I/flutter (25532): Observatory server failed to start after 3 tries I/flutter (25532): Observatory server failed to start after 4 tries I/flutter (25532): Observatory server failed to start after 5 tries I/flutter (25532): Observatory server failed to start after 6 tries I/flutter (25532): Observatory server failed to start after 7 tries I/flutter (25532): Observatory server failed to start after 8 tries I/flutter (25532): Observatory server failed to start after 9 tries I/flutter (25532): Observatory server failed to start after 10 tries I/flutter (25532): Observatory server failed to start after 11 tries I/flutter (25532): Could not start Observatory HTTP server: I/flutter (25532): SocketException: Failed to create server socket (OS Error: Permission denied, errno = 13), address = 127.0.0.1, port = 0 I/flutter (25532): #0 _NativeSocket.bind (dart:io-patch/socket_patch.dart:736:7) I/flutter (25532): <asynchronous suspension> I/flutter (25532): #1 _RawServerSocket.bind (dart:io-patch/socket_patch.dart:1394:26) I/flutter (25532): #2 _ServerSocket.bind (dart:io-patch/socket_patch.dart:1676:29) I/flutter (25532): #3 ServerSocket._bind (dart:io-patch/socket_patch.dart:1667:26) I/flutter (25532): #4 ServerSocket.bind (dart:io/socket.dart:382:27) I/flutter (25532): #5 _HttpServer.bind (dart:_http/http_impl.dart:2701:25) I/flutter (25532): #6 HttpServer.bind (dart:_http:229:19) I/flutter (25532): #7 Server.startup.poll (dart:vmservice_io/vmservice_server.dart:410:36) I/flutter (25532): <asynchronous suspension> I/flutter (25532): #8 Server.startup.poll (dart:vmservice_io/vmservice_server.dart) I/flutter (25532): #9 Server.startup (dart:vmservice_io/vmservice_server.dart:422:23) I/flutter (25532): <asynchronous suspension> I/flutter (25532): #10 main (dart:vmservice_io:262:12) I/flutter (25532):
原因分析
从log中我们可以看出,错误信息是Permission denied,创建socket服务器失败。做Android开发的同学一看就应该知道了,这是因为没有网络权限。
为什么需要创建服务器呢,这是为了能够让HotReload正常运行。每当代码一修改,保存后会自动把代码push到手机。
解决方法
在Android目录中的AndroidManifest.xml文件中,添加网络权限即可。
<uses-permission android:name="android.permission.INTERNET"/>