运行,调试以及热重载

Running, debugging, and hot reload

Once you’ve integrated the Flutter module to your project and used Flutter’splatform APIs to run the Flutter engine and/or UI, you can then build and runyour Android or iOS app the same way you run normal Android or iOS apps.

However, Flutter is now powering the UI in places where you’re showing aFlutterActivity or FlutterViewController.

Debugging

You may be used to having your suite of favorite Flutter debugging toolsavailable to you automatically when running flutter run or an equivalentcommand from an IDE. But you can also use all your Flutter debuggingfunctionalities such as hot reload, performanceoverlays, DevTools, and setting breakpoints in add-to-app scenarios.

These functionalities are provided by the flutter attach mechanism. flutterattach can be initiated through different pathways, such as through the SDK’sCLI tools, through VSCode or IntelliJ/Android Studio.

flutter attach via terminalflutter attach via terminal

flutter attach via VSCodeflutter attach via VSCode

flutter attach via IntelliJflutter attach via IntelliJ

flutter attach can connect as soon as you run your FlutterEngine, and willremain attached until your FlutterEngine is disposed. But you can invokeflutter attach before starting your engine. flutter attach will wait forthe next available Dart VM which will be hosted by your engine.

In IntelliJ or VSCode, you should select the device on which the Fluttermodule will run so flutter attach filters for the right start signals.

Debugging specific instances of Flutter

It’s possible to add multiple instances of Flutter (root isolates) to an app.flutter attach connects to all of the available isolates by default. Anycommands sent from the attached CLI are then forwarded to each of the attachedisolates.

You can list all the attached isolates by typing l from an attached flutterCLI tool. If unspecified, then the isolate names are automatically generatedfrom the dart entrypoint file and function name.

Example l output for an application that is displaying two Flutter isolatessimultaneously:

  1. Connected views:
  2. main.dart$main-517591213 (isolates/517591213)
  3. main.dart$main-332962855 (isolates/332962855)

In order to attach to specific isolates instead, do the following:

  • Name the Flutter root isolate of interest in its Dart source.
  1. // main.dart
  2. import 'dart:ui' as ui;
  3. void main() {
  4. ui.window.setIsolateDebugName("debug isolate");
  5. // ...
  6. }
  • Run flutter attach with the —isolate-filter option.
  1. $ flutter attach --isolate-filter='debug'
  2. Waiting for a connection from Flutter...
  3. Done.
  4. Syncing files to device... 1.1s
  5. 🔥 To hot reload changes while running, press "r". To hot restart (and rebuild state), press "R".
  6. An Observatory debugger and profiler is available at: http://127.0.0.1:43343/
  7. For a more detailed help message, press "h". To detach, press "d"; to quit, press "q".
  8. Connected view:
  9. debug isolate (isolates/642101161)