使用原生語言除錯器
如果你只用 Dart 程式碼撰寫 Flutter 應用程式,可以直接使用你的 IDE 除錯器來除錯程式碼。Flutter 團隊推薦使用 VS Code。
如果你撰寫平台專屬的 plugin 或使用平台專屬的函式庫,你可以使用原生除錯器來除錯這部分的程式碼。
- 若要除錯以 Swift 或 Objective-C 撰寫的 iOS 或 macOS 程式碼,可以使用 Xcode。
- 若要除錯以 Java 或 Kotlin 撰寫的 Android 程式碼,可以使用 Android Studio。
- 若要除錯以 C++ 撰寫的 Windows 程式碼,可以使用 Visual Studio。
本指南將說明如何為你的 Dart 應用程式同時連接「兩個」除錯器,一個用於 Dart,另一個用於原生程式碼。
除錯 Dart 程式碼
#本指南將說明如何使用 VS Code 來除錯你的 Flutter 應用程式。你也可以使用你偏好的 IDE,只要安裝並設定好 Flutter 與 Dart 外掛即可。
使用 VS Code 除錯 Dart 程式碼
#以下步驟說明如何使用 Dart 除錯器來除錯預設的 Flutter 範例應用程式。這些在 VS Code 中提供的功能元件,在你除錯自己的 Flutter 專案時同樣適用且會顯示。
建立一個基本的 Flutter 應用程式。
flutter create my_appCreating project my_app... Resolving dependencies in my_app... Got dependencies in my_app. Wrote 129 files. All done! You can find general documentation for Flutter at: https://docs.flutter.dev/ Detailed API documentation is available at: https://api.flutter.dev/ If you prefer video documentation, consider: https://www.youtube.com/c/flutterdev In order to run your application, type: $ cd my_app $ flutter run Your application code is in my_app/lib/main.dart.cd my_app在 Flutter 應用程式中,使用 VS Code 開啟
lib\main.dart檔案。點擊錯誤偵錯圖示(bug icon) (
)。 這會在 VS Code 中開啟以下面板:- Debug
- Debug Console
- Widget Inspector(元件檢查器)
第一次執行偵錯工具時,所需時間會比較長。
測試偵錯工具。
a. 在
main.dart中,點擊這一行:dart_counter++;b. 按下 Shift + F9。 這會在
_counter變數遞增的地方新增一個中斷點。c. 在應用程式中,點擊 + 按鈕 來遞增計數器。應用程式會暫停。
d. 此時,VS Code 會顯示:
- 在 編輯器群組(Editor Groups) 中:
main.dart中被標記的中斷點- Flutter 應用程式的元件階層(widget hierarchy), 顯示於 Widget Inspector 的 Widget Tree
- 在 側邊欄(side bar) 中:
- Call Stack 區段中的應用程式狀態
- Variables 區段中
this區域變數的值
- 在 面板(panel) 中:
- Debug console 中 Flutter 應用程式的日誌
- 在 編輯器群組(Editor Groups) 中:
VS Code Flutter 除錯器
#Flutter 外掛程式會為 VS Code 增加多項 使用者介面元件。
VS Code 介面變更
#啟動後,Flutter 除錯器會在 VS Code 介面中加入除錯工具。
下方的螢幕截圖與表格說明了各個工具的用途。

| 螢幕截圖標示顏色 | 列、面板或分頁 | 內容說明 |
|---|---|---|
| 黃色 | Variables | Flutter 應用程式中變數的目前值列表 |
| Watch | 你選擇追蹤的 Flutter 應用程式項目列表 | |
| Call Stack | Flutter 應用程式中作用中的子程序堆疊 | |
| Breakpoints | 你所設定的例外與中斷點列表 | |
| 綠色 | <Flutter files> | 你正在編輯的檔案 |
| 粉紅色 | Widget Inspector | 執行中的 Flutter 應用程式元件階層 |
| 藍色 | Layout Explorer | Flutter 如何在 Widget Inspector 中放置你所選元件的視覺化呈現 |
| Widget Details Tree | Widget Inspector 中所選元件的屬性列表 | |
| 橘色 | Problems | Dart 分析器在目前 Dart 檔案中發現的問題列表 |
| Output | 建置應用程式時 Flutter 應用程式回傳的回應 | |
| Debug Console | 除錯時 Flutter 應用程式產生的日誌或錯誤訊息 | |
| Terminal | VS Code 內建的系統命令列提示字元 |
若要變更 VS Code 中面板(橘色)的位置, 請前往 檢視(View) > 外觀(Appearance) > 面板位置(Panel Position)。
VS Code Flutter 除錯工具列
#此工具列可讓你使用任何除錯器進行除錯。 你可以逐步執行 Dart 陳述式、熱重載(hot reload)、或繼續執行應用程式。

| 圖示 | 動作 | 預設鍵盤快捷鍵 |
|---|---|---|
| 啟動或繼續執行 | F5 | |
| 暫停 | F6 | |
| 單步跳過(Step Over) | F10 | |
| 單步執行(Step Into) | F11 | |
| 單步跳出(Step Out) | Shift + F11 | |
| 熱重載(Hot Reload) | Ctrl + F5 | |
| 熱重啟(Hot Restart) | Shift + Special + F5 | |
| 停止 | Shift + F5 | |
| 開啟 Widget Inspector |
更新測試用 Flutter 應用程式
#在本指南的接下來部分,你需要更新 測試用 Flutter 應用程式。這次更新會加入可進行原生除錯的程式碼。
使用你偏好的 IDE 開啟
lib/main.dart檔案。用下列程式碼取代
main.dart的內容。展開以檢視本範例的 Flutter 程式碼
lib/main.dartdart// Copyright 2023 The Flutter Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. import 'package:flutter/material.dart'; import 'package:url_launcher/url_launcher.dart'; void main() { runApp(const MyApp()); } class MyApp extends StatelessWidget { const MyApp({super.key}); @override Widget build(BuildContext context) { return MaterialApp( title: 'URL Launcher', theme: ThemeData( colorSchemeSeed: Colors.purple, brightness: Brightness.light, ), home: const MyHomePage(title: 'URL Launcher'), ); } } class MyHomePage extends StatefulWidget { const MyHomePage({super.key, required this.title}); final String title; @override State<MyHomePage> createState() => _MyHomePageState(); } class _MyHomePageState extends State<MyHomePage> { Future<void>? _launched; Future<void> _launchInBrowser(Uri url) async { if (!await launchUrl( url, mode: LaunchMode.externalApplication, )) { throw Exception('Could not launch $url'); } } Future<void> _launchInWebView(Uri url) async { if (!await launchUrl( url, mode: LaunchMode.inAppWebView, )) { throw Exception('Could not launch $url'); } } Widget _launchStatus(BuildContext context, AsyncSnapshot<void> snapshot) { if (snapshot.hasError) { return Text('Error: ${snapshot.error}'); } else { return const Text(''); } } @override Widget build(BuildContext context) { final Uri toLaunch = Uri( scheme: 'https', host: 'docs.flutter.dev', path: 'testing/native-debugging'); return Scaffold( appBar: AppBar( title: Text(widget.title), ), body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[ Padding( padding: const EdgeInsets.all(16), child: Text(toLaunch.toString()), ), FilledButton( onPressed: () => setState(() { _launched = _launchInBrowser(toLaunch); }), child: const Text('Launch in browser'), ), const Padding(padding: EdgeInsets.all(16)), FilledButton( onPressed: () => setState(() { _launched = _launchInWebView(toLaunch); }), child: const Text('Launch in app'), ), const Padding(padding: EdgeInsets.all(16.0)), FutureBuilder<void>(future: _launched, builder: _launchStatus), ], ), ), ); } }若要將
url_launcher套件新增為相依套件,請執行flutter pub add:flutter pub add url_launcherResolving dependencies... collection 1.17.1 (1.17.2 available) + flutter_web_plugins 0.0.0 from sdk flutter matcher 0.12.15 (0.12.16 available) material_color_utilities 0.2.0 (0.8.0 available) + plugin_platform_interface 2.1.4 source_span 1.9.1 (1.10.0 available) stream_channel 2.1.1 (2.1.2 available) test_api 0.5.1 (0.6.1 available) + url_launcher 6.1.11 + url_launcher_android 6.0.36 + url_launcher_ios 6.1.4 + url_launcher_linux 3.0.5 + url_launcher_macos 3.0.5 + url_launcher_platform_interface 2.1.3 + url_launcher_web 2.0.17 + url_launcher_windows 3.0.6 Changed 10 dependencies!檢查程式碼庫有何變更:
在 Linux 或 macOS 上,執行此
find指令。find ./ -mmin -120./ios/Flutter/Debug.xcconfig ./ios/Flutter/Release.xcconfig ./linux/flutter/generated_plugin_registrant.cc ./linux/flutter/generated_plugins.cmake ./macos/Flutter/Flutter-Debug.xcconfig ./macos/Flutter/Flutter-Release.xcconfig ./macos/Flutter/GeneratedPluginRegistrant.swift ./pubspec.lock ./pubspec.yaml ./windows/flutter/generated_plugin_registrant.cc ./windows/flutter/generated_plugins.cmake在 Windows 中,請在命令提示字元(Command Prompt)執行以下指令。
Get-ChildItem C:\dev\example\ -Rescurse | Where-Object {$_.LastWriteTime -gt (Get-Date).AddDays(-1)}C:\dev\example\ios\Flutter\ Mode LastWriteTime Length Name ---- ------------- ------ ---- 8/1/2025 9:15 AM Debug.xcconfig 8/1/2025 9:15 AM Release.xcconfig C:\dev\example\linux\flutter\ Mode LastWriteTime Length Name ---- ------------- ------ ---- 8/1/2025 9:15 AM generated_plugin_registrant.cc 8/1/2025 9:15 AM generated_plugins.cmake C:\dev\example\macos\Flutter\ Mode LastWriteTime Length Name ---- ------------- ------ ---- 8/1/2025 9:15 AM Flutter-Debug.xcconfig 8/1/2025 9:15 AM Flutter-Release.xcconfig 8/1/2025 9:15 AM GeneratedPluginRegistrant.swift C:\dev\example\ Mode LastWriteTime Length Name ---- ------------- ------ ---- 8/1/2025 9:15 AM pubspec.lock 8/1/2025 9:15 AM pubspec.yaml C:\dev\example\windows\flutter\ Mode LastWriteTime Length Name ---- ------------- ------ ---- 8/1/2025 9:15 AM generated_plugin_registrant.cc 8/1/2025 9:15 AM generated_plugins.cmake
安裝 url_launcher 會在 Flutter 應用程式目錄中為所有目標平台新增設定檔與程式碼檔案。
同時偵錯 Dart 與原生語言程式碼
#本節說明如何同時使用 Flutter 的熱重載(hot reload)功能,來偵錯 Flutter 應用程式中的 Dart 程式碼,以及使用各自原生除錯器偵錯原生程式碼。此功能可讓你在編輯原生程式碼時,充分利用 Flutter 的熱重載。
使用 Android Studio 偵錯 Dart 與 Android 程式碼
#若要偵錯原生 Android 程式碼,你需要一個包含 Android 程式碼的 Flutter 應用程式。在本節中,你將學習如何將 Dart、Java 與 Kotlin 除錯器連接到你的應用程式。你不需要 VS Code 來同時偵錯 Dart 與 Android 程式碼。本指南包含 VS Code 的操作說明,是為了與 Xcode 及 Visual Studio 指南保持一致。
本節使用在 Update test Flutter app 中建立的相同範例 Flutter url_launcher 應用程式。
Build the Android version of the Flutter app in the Terminal
#To generate the needed Android platform dependencies, run the flutter build command.
flutter build appbundle --debugRunning Gradle task 'bundleDebug'... 27.1s
✓ Built build/app/outputs/bundle/debug/app-debug.aab.Start debugging with VS Code first
#If you use VS Code to debug most of your code, start with this section.
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.
Attach to the Flutter process in Android Studio
#Click the Attach debugger to Android process button. (
)The process dialog displays one entry for each connected device. Select show all processes to display available processes for each device.
Choose the process to which you want to attach. For this guide, select the
com.example.my_appprocess using the Emulator Pixel_5_API_33.Locate the tab for Android Debugger in the Debug pane.
In the Project pane, expand my_app_android > android > app > src > main > java > io.flutter plugins.
Double click GeneratedProjectRegistrant to open the Java code in the Edit pane.
At the end of this procedure, both the Dart and Android debuggers interact with the same process. Use either, or both, to set breakpoints, examine stack, resume execution and the like. In other words, debug!
Start debugging with Android Studio first
#If you use Android Studio to debug most of your code, start with this section.
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.
Click the Attach debugger to Android process button. (
)The process dialog displays one entry for each connected device. Select show all processes to display available processes for each device.
Choose the process to which you want to attach. For this guide, select the
com.example.my_appprocess using the Emulator Pixel_5_API_33.Locate the tab for Android Debugger in the Debug pane.
In the Project pane, expand my_app_android > android > app > src > main > java > io.flutter plugins.
Double click GeneratedProjectRegistrant to open the Java code in the Edit pane.
At the end of this procedure, both the Dart and Android debuggers interact with the same process. Use either, or both, to set breakpoints, examine stack, resume execution and the like. In other words, debug!
</device>
使用 Xcode 偵錯 Dart 與 iOS 程式碼
#若要偵錯 iOS 程式碼,你需要一個包含 iOS 程式碼的 Flutter 應用程式。在本節中,你將學習如何將兩個除錯器連接到你的應用程式:透過 VS Code 的 Flutter 除錯器,以及 Xcode。你需要同時執行 VS Code 與 Xcode。
本節使用在 Update test Flutter app 中建立的相同範例 Flutter url_launcher 應用程式。
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.
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.
使用 Xcode 偵錯 Dart 與 macOS 程式碼
#若要偵錯 macOS 程式碼,你需要一個包含 macOS 程式碼的 Flutter 應用程式。在本節中,你將學習如何將兩個除錯器連接到你的應用程式:透過 VS Code 的 Flutter 除錯器,以及 Xcode。你需要同時執行 VS Code 與 Xcode。
本節使用在 Update test Flutter app 中建立的相同範例 Flutter url_launcher 應用程式。
Build the macOS version of the Flutter app in the Terminal
#To generate the needed macOS platform dependencies, run the flutter build command.
flutter build macos --debugBuilding macOS application...Start debugging with VS Code first
#Start the 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.
Attach to the Flutter process in Xcode
#To attach to the Flutter app, go to Debug > Attach to Process > Runner.
Runner should be at the top of the Attach to Process menu under the Likely Targets heading.
Start debugging with Xcode first
#Start the debugger in Xcode
#Open
macos/Runner.xcworkspacefrom your Flutter app directory.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.
使用 Visual Studio 偵錯 Dart 與 C++ 程式碼
#若要偵錯 C++ 程式碼,你需要一個包含 C++ 程式碼的 Flutter 應用程式。在本節中,你將學習如何將兩個除錯器連接到你的應用程式:透過 VS Code 的 Flutter 除錯器,以及 Visual Studio。你需要同時執行 VS Code 與 Visual Studio。
本節使用在 Update test Flutter app 中建立的相同範例 Flutter url_launcher 應用程式。
Build the Windows version of the Flutter app in PowerShell or the Command Prompt
#To generate the needed Windows platform dependencies, run the flutter build command.
C:\> flutter build windows --debugBuilding Windows application... 31.4s
√ Built build\windows\runner\Debug\my_app.exe.Start debugging with VS Code first
#If you use VS Code to debug most of your code, start with this section.
Start the 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.
Attach to the Flutter process in Visual Studio
#To open the project solution file, go to File > Open > Project/Solution…
You can also press Ctrl + Shift + O.
Choose the
build/windows/my_app.slnfile in your Flutter app directory.Go to Debug > Attach to Process.
You can also press Ctrl + Alt + P.
From the Attach to Process dialog box, choose
my_app.exe.Visual Studio starts monitoring the Flutter app.
Start debugging with Visual Studio first
#If you use Visual Studio to debug most of your code, start with this section.
Start the local Windows debugger
#To open the project solution file, go to File > Open > Project/Solution…
You can also press Ctrl + Shift + O.
Choose the
build/windows/my_app.slnfile in your Flutter app directory.Set
my_appas the startup project. In the Solution Explorer, right-click onmy_appand select Set as Startup Project.Click Local Windows Debugger to start debugging.
You can also press F5.
When the Flutter app has started, a console window displays a message with the Dart VM service URI. It resembles the following response:
flutter: The Dart VM service is listening on http://127.0.0.1:62080/KPHEj2qPD1E=/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 Visual Studio and press Enter.
相關資源
#歡迎參考以下關於 Flutter、iOS、Android、macOS 與 Windows 偵錯的資源:
Flutter
#Android
#你可以在 developer.android.com 找到以下偵錯資源。
iOS 與 macOS
#你可以在 developer.apple.com 找到以下偵錯資源。
Windows
#你可以在 Microsoft Learn 找到 Windows 偵錯相關資源。