运行flutter web应用程序时无法创建服务器套接字

cotxawn7  于 2023-04-22  发布在  Flutter
关注(0)|答案(3)|浏览(142)

我正在做一个flutter项目。它在android模拟器上运行得很好,但是当我想作为web应用程序运行时,我面临这个问题:

Launching lib\main.dart on Chrome in debug mode...
Finished with error: Failed to bind web development server:
SocketException: Failed to create server socket (OS Error: Failed to start accept), address = 
localhost, port = 5403

我尝试运行其他简单的项目,但我面临同样的错误.我做的每一步根据Flutter官方文档.这是我的Flutter医生:

Doctor summary (to see all details, run flutter doctor -v):
[√] Flutter (Channel beta, v1.17.0, on Microsoft Windows [Version 10.0.18363.778], locale en-US)

[√] Android toolchain - develop for Android devices (Android SDK version 30.0.0-rc2)
[√] Chrome - develop for the web
[√] Android Studio (version 3.6)
[√] IntelliJ IDEA Ultimate Edition (version 2019.3)
[√] VS Code (version 1.44.2)
[√] Connected device (3 available)

• No issues found!

我已经在网上搜索过了,但是没有任何好的解决这个问题的办法。
你能帮帮我吗?
先谢谢你了。

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
  theme: ThemeData(
    primarySwatch: Colors.blue,
    visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
  _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
          'You have pushed the button this many times:',
        ),
        Text(
          '$_counter',
          style: Theme.of(context).textTheme.headline4,
        ),
      ],
    ),
  ),
  floatingActionButton: FloatingActionButton(
    onPressed: _incrementCounter,
        tooltip: 'Increment',
    child: Icon(Icons.add)
      ),
    );
  }
}

我以管理员身份在命令窗口中运行flutter -v -d chrome run,这是响应:

[  +32 ms] executing: [C:\android\flutter/] git -c log.showSignature=false log -n 1 --pretty=format:%H
[ +129 ms] Exit code 0 from: git -c log.showSignature=false log -n 1 --pretty=format:%H
[   +2 ms] d3ed9ec945f8869f0e136c357d0c2a6be2b60c98
[        ] executing: [C:\android\flutter/] git describe --match *.*.* --first-parent --long --tags
[ +123 ms] Exit code 0 from: git describe --match *.*.* --first-parent --long --tags
[        ] 1.17.0-dev.3.1-0-gd3ed9ec94
[  +16 ms] executing: [C:\android\flutter/] git rev-parse --abbrev-ref --symbolic @{u}
[  +96 ms] Exit code 0 from: git rev-parse --abbrev-ref --symbolic @{u}
[        ] origin/beta
[        ] executing: [C:\android\flutter/] git ls-remote --get-url origin
[  +87 ms] Exit code 0 from: git ls-remote --get-url origin
[        ] https://github.com/flutter/flutter.git
[ +254 ms] executing: [C:\android\flutter/] git rev-parse --abbrev-ref HEAD
[  +97 ms] Exit code 0 from: git rev-parse --abbrev-ref HEAD
[   +2 ms] beta
[  +58 ms] Artifact Instance of 'AndroidMavenArtifacts' is not required, skipping update.
[   +1 ms] Artifact Instance of 'AndroidGenSnapshotArtifacts' is not required, skipping update.
[   +2 ms] Artifact Instance of 'AndroidInternalBuildArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'IOSEngineArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'FlutterWebSdk' is not required, skipping update.
[   +7 ms] Artifact Instance of 'WindowsEngineArtifacts' is not required, skipping update.
[   +1 ms] Artifact Instance of 'MacOSEngineArtifacts' is not required, skipping update.
[   +2 ms] Artifact Instance of 'LinuxEngineArtifacts' is not required, skipping update.
[   +1 ms] Artifact Instance of 'LinuxFuchsiaSDKArtifacts' is not required, skipping update.
[   +1 ms] Artifact Instance of 'MacOSFuchsiaSDKArtifacts' is not required, skipping update.
[   +1 ms] Artifact Instance of 'FlutterRunnerSDKArtifacts' is not required, skipping update.
[   +2 ms] Artifact Instance of 'FlutterRunnerDebugSymbols' is not required, skipping update.
[  +30 ms] executing: C:\android\sdk\platform-tools\adb.exe devices -l
[  +90 ms] List of devices attached
           emulator-5554          device product:sdk_google_phone_x86 model:Android_SDK_built_for_x86 device:generic_x86
           transport_id:2
[  +20 ms] Artifact Instance of 'AndroidMavenArtifacts' is not required, skipping update.
[   +1 ms] Artifact Instance of 'AndroidGenSnapshotArtifacts' is not required, skipping update.
[   +1 ms] Artifact Instance of 'AndroidInternalBuildArtifacts' is not required, skipping update.
[   +2 ms] Artifact Instance of 'IOSEngineArtifacts' is not required, skipping update.
[   +8 ms] Artifact Instance of 'WindowsEngineArtifacts' is not required, skipping update.
[   +1 ms] Artifact Instance of 'MacOSEngineArtifacts' is not required, skipping update.
[   +2 ms] Artifact Instance of 'LinuxEngineArtifacts' is not required, skipping update.
[   +1 ms] Artifact Instance of 'LinuxFuchsiaSDKArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'MacOSFuchsiaSDKArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'FlutterRunnerSDKArtifacts' is not required, skipping update.
[   +2 ms] Artifact Instance of 'FlutterRunnerDebugSymbols' is not required, skipping update.
[ +348 ms] Generating C:\AndroidStudioProjects\Samsung
mobile\flutter_app\android\app\src\main\java\io\flutter\plugins\GeneratedPluginRegistrant.java
[ +170 ms] Launching lib\main.dart on Chrome in debug mode...
[  +73 ms] "flutter run" took 948ms.
Failed to bind web development server:
SocketException: Failed to create server socket (OS Error: Failed to start accept), address = localhost, port = 13632

#0      throwToolExit (package:flutter_tools/src/base/common.dart:14:3)
#1      WebAssetServer.start (package:flutter_tools/src/build_runner/devfs_web.dart:221:7)
<asynchronous suspension>
#2      WebDevFS.create (package:flutter_tools/src/build_runner/devfs_web.dart:605:43)
#3      _ResidentWebRunner.run.<anonymous closure>
(package:flutter_tools/src/build_runner/resident_web_runner.dart:426:44)
<asynchronous suspension>
#4      _ResidentWebRunner.run.<anonymous closure> (package:flutter_tools/src/build_runner/resident_web_runner.dart)
#5      asyncGuard.<anonymous closure> (package:flutter_tools/src/base/async_guard.dart:111:32)
#6      _rootRun (dart:async/zone.dart:1184:13)
#7      _CustomZone.run (dart:async/zone.dart:1077:19)
#8      _runZoned (dart:async/zone.dart:1619:10)
#9      runZonedGuarded (dart:async/zone.dart:1608:12)
#10     runZoned (dart:async/zone.dart:1536:12)
#11     asyncGuard (package:flutter_tools/src/base/async_guard.dart:109:3)
#12     _ResidentWebRunner.run (package:flutter_tools/src/build_runner/resident_web_runner.dart:407:20)
<asynchronous suspension>
#13     RunCommand.runCommand (package:flutter_tools/src/commands/run.dart:564:37)
<asynchronous suspension>
#14     FlutterCommand.verifyThenRunCommand (package:flutter_tools/src/runner/flutter_command.dart:722:18)
#15     _rootRunUnary (dart:async/zone.dart:1192:38)
#16     _CustomZone.runUnary (dart:async/zone.dart:1085:19)
#17     _FutureListener.handleValue (dart:async/future_impl.dart:141:18)
#18     Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:682:45)
#19     Future._propagateToListeners (dart:async/future_impl.dart:711:32)
#20     Future._completeWithValue (dart:async/future_impl.dart:526:5)
#21     _AsyncAwaitCompleter.complete (dart:async-patch/async_patch.dart:36:15)
#22     _completeOnAsyncReturn (dart:async-patch/async_patch.dart:298:13)
#23     RunCommand.usageValues (package:flutter_tools/src/commands/run.dart)
#24     _rootRunUnary (dart:async/zone.dart:1192:38)
#25     _CustomZone.runUnary (dart:async/zone.dart:1085:19)
#26     _FutureListener.handleValue (dart:async/future_impl.dart:141:18)
#27     Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:682:45)
#28     Future._propagateToListeners (dart:async/future_impl.dart:711:32)
#29     Future._completeWithValue (dart:async/future_impl.dart:526:5)
#30     Future._asyncComplete.<anonymous closure> (dart:async/future_impl.dart:556:7)
#31     _rootRun (dart:async/zone.dart:1184:13)
#32     _CustomZone.run (dart:async/zone.dart:1077:19)
#33     _CustomZone.runGuarded (dart:async/zone.dart:979:7)
#34     _CustomZone.bindCallbackGuarded.<anonymous closure> (dart:async/zone.dart:1019:23)
#35     _microtaskLoop (dart:async/schedule_microtask.dart:43:21)
#36     _startMicrotaskLoop (dart:async/schedule_microtask.dart:52:5)
#37     _runPendingImmediateCallback (dart:isolate-patch/isolate_patch.dart:118:13)
#38     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:169:5)
b4lqfgs4

b4lqfgs41#

对于使用VS Code的人。您可以将这些参数添加到lunch.json文件中。这样,您只需按F5并调试您的应用程序。此文件应该在.vscode文件夹中。如果不在,请创建它。它应该看起来像这样。

{
  "version": "0.2.0",
  "configurations": [
    {
        "name": "Flutter",
        "request": "launch",
        "type": "dart",
        "args": [
            "--web-hostname",
            "127.0.0.1"
        ]
    }
  ]
}
yeotifhr

yeotifhr2#

如果你不想每次运行应用程序时都在终端中输入上述命令,请设置一次:
Simply head over to Run/Debug configurations
go to additional run args and enter: --web-hostname=127.0.0.1
这样你仍然连接到Android Studio。
您可以点击上面的链接查看图像说明。

--web-hostname=127.0.0.1
5us2dqdw

5us2dqdw3#

我解决了这个问题。在我的情况下我需要确定端口

flutter run -d chrome --web-port=8080 --web-hostname=127.0.0.1

在终端中使用此代码。

相关问题