在 iOS 模拟器中启动 Flutter 应用程序时出错

Errors Launching Flutter app in iOS Simulator

我在 iOS 模拟器上启动 Flutter 应用程序时遇到问题。从以下错误输出判断,该问题似乎与本地主机连接等有关,但我无法找到解决方法。

我是 运行 MacOS Catalina 版本 10.15.6 (19G73)。 iOS 模拟器版本 11.6 (921.9.1)。 VSCode 是我的 IDE.

该应用程序在 Android 对应的模拟器上启动并运行良好。

下面是 VSCode 终端的错误输出:

Launching lib/main.dart on iPhone SE (2nd generation) in debug mode...
Running Xcode build...                                                  
 └─Compiling, linking and signing...                         6.7s
Xcode build done.                                           16.7s
Connecting to the VM Service is taking longer than expected...
Still attempting to connect to the VM Service...
If you do NOT see the Flutter application running, it might have crashed. The device logs (e.g. from adb or XCode) might have more details.
If you do see the Flutter application running on the device, try re-running with --host-vmservice-port to use a specific port known to be available.
Exception attempting to connect to the VM Service: SocketException: OS Error: Connection refused, errno = 61, address = 127.0.0.1, port = 51838
This was attempt #50. Will retry in 0:00:01.600000.
Exception attempting to connect to the VM Service: SocketException: OS Error: Connection refused, errno = 61, address = 127.0.0.1, port = 51970
This was attempt #100. Will retry in 0:00:01.600000.
Exception attempting to connect to the VM Service: SocketException: OS Error: Connection refused, errno = 61, address = 127.0.0.1, port = 52119
This was attempt #150. Will retry in 0:00:01.600000.
Exception attempting to connect to the VM Service: SocketException: OS Error: Connection refused, errno = 61, address = 127.0.0.1, port = 52347
This was attempt #200. Will retry in 0:00:01.600000.
^C%

由于 Firebase,我遇到了同样的问题。 我通过反转 AppDelegate.swift 文件中的 2 行解决了我的问题:

FirebaseApp.configure() // Should be first
GeneratedPluginRegistrant.register(with: self) // Should be second

我的非工作文件是这个

import UIKit
import Flutter
import Firebase

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
  override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  ) -> Bool {
    GeneratedPluginRegistrant.register(with: self) // <- Don't do this
    FirebaseApp.configure()   // <- Don't do this
    return super.application(application, didFinishLaunchingWithOptions: launchOptions)
  }
}

我的工作文件是:

import UIKit
import Flutter
import Firebase

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
  override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  ) -> Bool {
    FirebaseApp.configure()   // <- Do this
    GeneratedPluginRegistrant.register(with: self) // <- Do this
    return super.application(application, didFinishLaunchingWithOptions: launchOptions)
  }
}

我遇到了同样的问题,在iOS模拟器中无法正常运行应用程序,它总是显示一个白色的空白页面。

我找到了这个https://github.com/flutter/flutter/issues/71395,这个link告诉我们切换Flutter git分支到master如果你的Flutter版本低于2.0.0,那么你会得到正确的消息在 master 分支。

所以我通过以下步骤解决了问题:

  1. 获取Flutter路径,我用的是brew
brew info flutter
  1. 将 Flutter 的 git 分支切换到 master
cd <flutter path>
git checkout -b master origin/master
  1. 找到问题
flutter doctor -v

说明我的问题是

[!] Proxy Configuration

• HTTP_PROXY is set

! NO_PROXY is not set

所以我需要设置 NO_PROXY 配置,https://github.com/flutter/flutter/issues/24854,这个 link 告诉我如何配置它。

现在我可以在 iOS 模拟器中正常 运行 应用程序了。