尝试为 flutter_web 项目提供服务时无法创建本地服务器(地址已被使用)

failed to create local server ( address already in use ) when trying to serve flutter_web project

我尝试通过这个终端命令在本地启动 flutter web 项目

 webdev serve --auto restart

但我收到以下错误消息

    Starting resource servers...Unhandled exception:
SocketException: Failed to create server socket (OS Error: Address already in use, errno = 48), address = 127.0.0.1, port = 8080
#0      _NativeSocket.bind (dart:io-patch/socket_patch.dart:591:7)
<asynchronous suspension>
#1      _RawServerSocket.bind (dart:io-patch/socket_patch.dart:1206:26)
#2      _ServerSocket.bind (dart:io-patch/socket_patch.dart:1466:29)
#3      ServerSocket.bind (dart:io-patch/socket_patch.dart:1457:26)
#4      _HttpServer.bind (dart:_http/http_impl.dart:2520:25)
#5      HttpServer.bind (dart:_http:227:19)
#6      HttpMultiServer.loopback.<anonymous closure> (package:http_multi_server/http_multi_server.dart:113:39)
#7      HttpMultiServer._loopback (package:http_multi_server/http_multi_server.dart:172:30)
<asynchronous suspension>
#8      HttpMultiServer.loopback (package:http_multi_server/http_multi_server.dart:111:12)
#9      WebDevServer.start (package:webdev/src/serve/webdev_server.dart:83:33)
#10     _AsyncAwaitCompleter.start (dart:async-patch/async_patch.dart:49:6)
#11     WebDevServer.start (package:webdev/src/serve/webdev_server.dart:54:36)
#12     ServerManager.start (package:webdev/src/serve/server_manager.dart:22:38)
#13     _AsyncAwaitCompleter.start (dart:async-patch/async_patch.dart:49:6)
#14     ServerManager.start (package:webdev/src/serve/server_manager.dart:18:37)
#15     _startServerManager (package:webdev/src/serve/dev_workflow.dart:85:27)
#16     _AsyncAwaitCompleter.start (dart:async-patch/async_patch.dart:49:6)
#17     _startServerManager (package:webdev/src/serve/dev_workflow.dart:66:42)
#18     DevWorkflow.start (package:webdev/src/serve/dev_workflow.dart:186:31)
#19     _RootZone.runUnary (dart:async/zone.dart:1379:54)
#20     _FutureListener.handleValue (dart:async/future_impl.dart:126:18)
#21     Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:639:45)
#22     Future._propagateToListeners (dart:async/future_impl.dart:668:32)
#23     Future._complete (dart:async/future_impl.dart:473:7)
#24     _SyncCompleter.complete (dart:async/future_impl.dart:51:12)
#25     _AsyncAwaitCompleter.complete.<anonymous closure> (dart:async-patch/async_patch.dart:33:20)
#26     _microtaskLoop (dart:async/schedule_microtask.dart:41:21)
#27     _startMicrotaskLoop (dart:async/schedule_microtask.dart:50:5)
#28     _runPendingImmediateCallback (dart:isolate-patch/isolate_patch.dart:115:13)
#29     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:172:5)
abdullah@Abdullahs-MacBook-Pro ~/D/F/web_profile> webdev serve --auto restart
[SEVERE] Deleted previous snapshot due to missing asset graph.
[INFO] Building new asset graph completed, took 1.4s
[INFO] Checking for unexpected pre-existing outputs. completed, took 1ms
[INFO] Starting resource servers...Unhandled exception:
SocketException: Failed to create server socket (OS Error: Address already in use, errno = 48), address = 127.0.0.1, port = 8080
#0      _NativeSocket.bind (dart:io-patch/socket_patch.dart:591:7)
<asynchronous suspension>
#1      _RawServerSocket.bind (dart:io-patch/socket_patch.dart:1206:26)
#2      _ServerSocket.bind (dart:io-patch/socket_patch.dart:1466:29)
#3      ServerSocket.bind (dart:io-patch/socket_patch.dart:1457:26)
#4      _HttpServer.bind (dart:_http/http_impl.dart:2520:25)
#5      HttpServer.bind (dart:_http:227:19)
#6      HttpMultiServer.loopback.<anonymous closure> (package:http_multi_server/http_multi_server.dart:113:39)
#7      HttpMultiServer._loopback (package:http_multi_server/http_multi_server.dart:172:30)
<asynchronous suspension>
#8      HttpMultiServer.loopback (package:http_multi_server/http_multi_server.dart:111:12)
#9      WebDevServer.start (package:webdev/src/serve/webdev_server.dart:83:33)
#10     _AsyncAwaitCompleter.start (dart:async-patch/async_patch.dart:49:6)
#11     WebDevServer.start (package:webdev/src/serve/webdev_server.dart:54:36)
#12     ServerManager.start (package:webdev/src/serve/server_manager.dart:22:38)
#13     _AsyncAwaitCompleter.start (dart:async-patch/async_patch.dart:49:6)
#14     ServerManager.start (package:webdev/src/serve/server_manager.dart:18:37)
#15     _startServerManager (package:webdev/src/serve/dev_workflow.dart:85:27)
#16     _AsyncAwaitCompleter.start (dart:async-patch/async_patch.dart:49:6)
#17     _startServerManager (package:webdev/src/serve/dev_workflow.dart:66:42)
#18     DevWorkflow.start (package:webdev/src/serve/dev_workflow.dart:186:31)
#19     _RootZone.runUnary (dart:async/zone.dart:1379:54)
#20     _FutureListener.handleValue (dart:async/future_impl.dart:126:18)
#21     Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:639:45)
#22     Future._propagateToListeners (dart:async/future_impl.dart:668:32)
#23     Future._complete (dart:async/future_impl.dart:473:7)
#24     _SyncCompleter.complete (dart:async/future_impl.dart:51:12)
#25     _AsyncAwaitCompleter.complete.<anonymous closure> (dart:async-patch/async_patch.dart:33:20)
#26     _microtaskLoop (dart:async/schedule_microtask.dart:41:21)
#27     _startMicrotaskLoop (dart:async/schedule_microtask.dart:50:5)
#28     _runPendingImmediateCallback (dart:isolate-patch/isolate_patch.dart:115:13)
#29     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:172:5)
abdullah@Abdullahs-MacBook-Pro ~/D/F/web_profile> webdev serve --auto restart
[SEVERE] Deleted previous snapshot due to missing asset graph.
[INFO] Starting resource servers...Unhandled exception:
SocketException: Failed to create server socket (OS Error: Address already in use, errno = 48), address = 127.0.0.1, port = 8080
#0      _NativeSocket.bind (dart:io-patch/socket_patch.dart:591:7)
<asynchronous suspension>
#1      _RawServerSocket.bind (dart:io-patch/socket_patch.dart:1206:26)
#2      _ServerSocket.bind (dart:io-patch/socket_patch.dart:1466:29)
#3      ServerSocket.bind (dart:io-patch/socket_patch.dart:1457:26)
#4      _HttpServer.bind (dart:_http/http_impl.dart:2520:25)
#5      HttpServer.bind (dart:_http:227:19)
#6      HttpMultiServer.loopback.<anonymous closure> (package:http_multi_server/http_multi_server.dart:113:39)
#7      HttpMultiServer._loopback (package:http_multi_server/http_multi_server.dart:172:30)
<asynchronous suspension>
#8      HttpMultiServer.loopback (package:http_multi_server/http_multi_server.dart:111:12)
#9      WebDevServer.start (package:webdev/src/serve/webdev_server.dart:83:33)
#10     _AsyncAwaitCompleter.start (dart:async-patch/async_patch.dart:49:6)
#11     WebDevServer.start (package:webdev/src/serve/webdev_server.dart:54:36)
#12     ServerManager.start (package:webdev/src/serve/server_manager.dart:22:38)
#13     _AsyncAwaitCompleter.start (dart:async-patch/async_patch.dart:49:6)
#14     ServerManager.start (package:webdev/src/serve/server_manager.dart:18:37)
#15     _startServerManager (package:webdev/src/serve/dev_workflow.dart:85:27)
#16     _AsyncAwaitCompleter.start (dart:async-patch/async_patch.dart:49:6)
#17     _startServerManager (package:webdev/src/serve/dev_workflow.dart:66:42)
#18     DevWorkflow.start (package:webdev/src/serve/dev_workflow.dart:186:31)
#19     _RootZone.runUnary (dart:async/zone.dart:1379:54)
#20     _FutureListener.handleValue (dart:async/future_impl.dart:126:18)
#21     Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:639:45)
#22     Future._propagateToListeners (dart:async/future_impl.dart:668:32)
abdullah@Abdullahs-MacBook-Pro ~/D/F/web_profile> webdev serve --auto restart
[SEVERE] Deleted previous snapshot due to missing asset graph.
[INFO] Building new asset graph completed, took 1.3s
[INFO] Checking for unexpected pre-existing outputs. completed, took 1ms
[INFO] Starting resource servers...Unhandled exception:
SocketException: Failed to create server socket (OS Error: Address already in use, errno = 48), address = 127.0.0.1, port = 8080
#0      _NativeSocket.bind (dart:io-patch/socket_patch.dart:591:7)
<asynchronous suspension>
#1      _RawServerSocket.bind (dart:io-patch/socket_patch.dart:1206:26)
#2      _ServerSocket.bind (dart:io-patch/socket_patch.dart:1466:29)
#3      ServerSocket.bind (dart:io-patch/socket_patch.dart:1457:26)
#4      _HttpServer.bind (dart:_http/http_impl.dart:2520:25)
#5      HttpServer.bind (dart:_http:227:19)
#6      HttpMultiServer.loopback.<anonymous closure> (package:http_multi_server/http_multi_server.dart:113:39)
#7      HttpMultiServer._loopback (package:http_multi_server/http_multi_server.dart:172:30)
<asynchronous suspension>
#8      HttpMultiServer.loopback (package:http_multi_server/http_multi_server.dart:111:12)
#9      WebDevServer.start (package:webdev/src/serve/webdev_server.dart:83:33)
#10     _AsyncAwaitCompleter.start (dart:async-patch/async_patch.dart:49:6)
#11     WebDevServer.start (package:webdev/src/serve/webdev_server.dart:54:36)
#12     ServerManager.start (package:webdev/src/serve/server_manager.dart:22:38)
#13     _AsyncAwaitCompleter.start (dart:async-patch/async_patch.dart:49:6)
#14     ServerManager.start (package:webdev/src/serve/server_manager.dart:18:37)
#15     _startServerManager (package:webdev/src/serve/dev_workflow.dart:85:27)
#16     _AsyncAwaitCompleter.start (dart:async-patch/async_patch.dart:49:6)
#17     _startServerManager (package:webdev/src/serve/dev_workflow.dart:66:42)
#18     DevWorkflow.start (package:webdev/src/serve/dev_workflow.dart:186:31)
#19     _RootZone.runUnary (dart:async/zone.dart:1379:54)
#20     _FutureListener.handleValue (dart:async/future_impl.dart:126:18)
#21     Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:639:45)
#22     Future._propagateToListeners (dart:async/future_impl.dart:668:32)

我试图通过从 activity monitor 强制退出 dart 进程来解决这个问题, 但我仍然得到同样的错误

是否可以将端口号从 8080 更改为任意端口号?

或者在给定的默认端口不可用时自动更改为端口号?

简答。

webdev serve web:8082

其中 web 是目录名称,8082 是端口号。