偵錯你的 add-to-app 模組
當你已將 Flutter 模組整合進你的專案,並且透過 Flutter 的平台 API 執行 Flutter 引擎與/或 UI 之後, 你就可以像執行一般 Android 或 iOS 應用程式一樣,建置並執行你的 Android 或 iOS 應用程式。
只要你的程式碼中包含 FlutterActivity 或 FlutterViewController, Flutter 就會負責該處的 UI。
概覽
#你可能已經習慣在執行 flutter run 或從 IDE 執行等效指令時, 使用你最愛的 Flutter 偵錯工具組。 但在 add-to-app 的情境下,你同樣可以使用所有 Flutter 偵錯功能, 例如熱重載、效能疊加層、DevTools,以及設定中斷點等。
flutter attach 指令提供了這些功能。 你可以使用 SDK 的命令列工具(CLI tools)、VS Code、 IntelliJ IDEA 或 Android Studio 來執行這個指令。
當你執行 FlutterEngine 時,flutter attach 指令會連線。 它會一直保持連線,直到你釋放 FlutterEngine 為止。 你可以在啟動引擎前呼叫 flutter attach。 flutter attach 指令會等待你的引擎所啟動的下一個可用 Dart VM。
從終端機偵錯
#若要從終端機附加偵錯,請執行 flutter attach。 若要選擇特定目標裝置,請加上 -d <deviceId>。
flutter attach該指令應該會輸出類似以下的內容:
Syncing files to device iPhone 15 Pro...
7,738ms (!)
To hot reload the changes while running, press "r".
To hot restart (and rebuild state). press "R".在 Xcode 與 VS Code 偵錯 iOS 擴充套件
#Build the iOS version of the Flutter app in the Terminal
#To generate the needed iOS platform dependencies, run the flutter build command.
flutter build ios --config-only --no-codesign --debugWarning: Building for device with codesigning disabled. You will have to manually codesign before deploying to device.
Building com.example.myApp for device (ios)...Start debugging with VS Code first
#If you use VS Code to debug most of your code, start with this section.
Start the Dart debugger in VS Code
#To open the Flutter app directory, go to File > Open Folder... and choose the
my_appdirectory.Open the
lib/main.dartfile.If you can build an app for more than one device, you must select the device first.
Go to View > Command Palette...
You can also press Ctrl / Cmd + Shift + P.
Type
flutter select.Click the Flutter: Select Device command.
Choose your target device.
Click the debug icon (
). This opens the Debug pane and launches the app. Wait for the app to launch on the device and for the debug pane to indicate Connected. The debugger takes longer to launch the first time. Subsequent launches start faster.This Flutter app contains two buttons:
- Launch in browser: This button opens this page in the default browser of your device.
- Launch in app: This button opens this page within your app. This button only works for iOS or Android. Desktop apps launch a browser.
Enable automatic attachment
#You can configure VS Code to attach to your Flutter module project whenever you start debugging. To enable this feature, create a .vscode/launch.json file in your Flutter module project.
Go to View > Run.
You can also press Ctrl / Cmd + Shift + D.
VS Code displays the Run and Debug sidebar.
In this sidebar, click create a launch.json file.
VS Code displays the Select debugger menu at the top.
Select Dart & Flutter.
VS Code creates then opens the
.vscode/launch.jsonfile.Expand to see an example launch.json file
json{ // Use IntelliSense to learn about possible attributes. // Hover to view descriptions of existing attributes. // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ { "name": "my_app", "request": "launch", "type": "dart" }, { "name": "my_app (profile mode)", "request": "launch", "type": "dart", "flutterMode": "profile" }, { "name": "my_app (release mode)", "request": "launch", "type": "dart", "flutterMode": "release" } ] }To attach, go to Run > Start Debugging.
You can also press F5.
Attach to the Flutter process in Xcode
#To attach to the Flutter app in Xcode:
Go to Debug > Attach to Process >
Select Runner. It should be at the top of the Attach to Process menu under the Likely Targets heading.
Start debugging with Xcode first
#If you use Xcode to debug most of your code, start with this section.
Start the Xcode debugger
#Open
ios/Runner.xcworkspacefrom your Flutter app directory.Select the correct device using the Scheme menu in the toolbar.
If you have no preference, choose iPhone Pro 14.
Run this Runner as a normal app in Xcode.
When the run completes, the Debug area at the bottom of Xcode displays a message with the Dart VM service URI. It resembles the following response:
2023-07-12 14:55:39.966191-0500 Runner[58361:53017145] flutter: The Dart VM service is listening on http://127.0.0.1:50642/00wEOvfyff8=/Copy the Dart VM service URI.
Attach to the Dart VM in VS Code
#To open the command palette, go to View > Command Palette...
You can also press Cmd + Shift + P.
Type
debug.Click the Debug: Attach to Flutter on Device command.
In the Paste an VM Service URI box, paste the URI you copied from Xcode and press Enter.
在 Android Studio 偵錯 Android 擴充套件
#To open the Flutter app directory, go to File > Open... and choose the
my_appdirectory.Open the
lib/main.dartfile.Choose a virtual Android device. Go to the toolbar, open the leftmost dropdown menu, and click on Open Android Emulator: <device>.
You can choose any installed emulator that's doesn't include
arm64.From that same menu, select the virtual Android device.
From the toolbar, click Run 'main.dart'.
You can also press Ctrl + Shift + R.
After the app displays in the emulator, continue to the next step.
無需 USB 連線進行偵錯
#若要在 iOS 或 Android 裝置上透過 Wi-Fi 偵錯您的應用程式, 請使用 flutter attach。
在 iOS 裝置上透過 Wi-Fi 偵錯
#針對 iOS 目標,請完成以下步驟:
確認您的裝置已如 iOS 設定指南 所述,透過 Wi-Fi 連接至 Xcode。
在您的 macOS 開發機上, 開啟 Xcode > Product > Scheme > Edit Scheme...。
您也可以按下 Cmd + <。
點擊 Run。
點擊 Arguments。
在 Arguments Passed On Launch 中,點擊 +。
如果您的開發機使用 IPv4,請加入
--vm-service-host=0.0.0.0。如果您的開發機使用 IPv6,請加入
--vm-service-host=::0。

如何判斷您是否處於 IPv6 網路
#開啟 設定 > Wi-Fi。
點擊您已連線的網路。
點擊 詳細資訊...
點擊 TCP/IP。
檢查是否有 IPv6 位址 區段。

在 Android 裝置上透過 Wi-Fi 偵錯
#請確認您的裝置已如 Android 設定指南 所述,透過 Wi-Fi 連接至 Android Studio。