Sub topic 9
Debugging an Embedded Flutter Module
Assume you know basic Dart and Flutter, and that you can build and run an Android or iOS host app. You also need access to the native project and a Flutter module that the host already opens. This chapter focuses on debugging the Flutter part while it runs inside the native application.
The embedded debug workflow
A Flutter module does not start from the usual flutter run command when the native application owns the process. Instead, the native app starts a FlutterEngine, and Flutter runs inside that engine. In debug mode, the engine exposes a connection that the Flutter CLI can use for hot reload and DevTools.
The usual workflow is:
- Start the native app from Android Studio or Xcode in debug mode.
- Navigate to a screen that displays Flutter content.
- Open a terminal in the Flutter module directory.
- Run
flutter attach. - Make Dart changes and use hot reload.
cd my_flutter_module
flutter attachThe CLI searches for a running debuggable Flutter engine. After it connects, it displays commands such as r for hot reload, R for hot restart, and q to disconnect.
Hot reload updates the Dart code and usually preserves the current widget state. Hot restart runs the Dart entrypoint again and clears that state. Restart is useful when you change initialization code, global state, or an entrypoint.
Reading logs and finding failures
Use native tools for native failures and Flutter tools for Dart failures.
- Android logs appear in Logcat. Filter by your application process or by
flutter. - iOS logs appear in Xcode's debug console or the macOS Console application.
- Dart exceptions appear in the terminal running
flutter attach, the IDE debug console, or DevTools. - Flutter framework errors often include a widget name and a Dart stack trace.
A channel call can fail even when the Flutter UI works. For example, Dart may invoke a method that the native host has not registered. Catch the exception near the call so the screen shows a useful state instead of failing silently.
import 'package:flutter/services.dart';
const sessionChannel = MethodChannel('com.example.host/session');
Future<String> loadUserName() async {
try {
return await sessionChannel.invokeMethod<String>('getUserName') ?? 'guest';
} on PlatformException catch (error) {
debugPrint('Unable to load user name: ${error.code}');
return 'guest';
} on MissingPluginException {
debugPrint('The host registered no session channel');
return 'guest';
}
}The channel name, method name, and expected value type must match the native implementation. A MissingPluginException commonly means that the wrong engine was configured, the plugin registration did not run, or the native handler was registered after the relevant call.
Common `flutter attach` problems
If the CLI cannot find the application, check these points:
- The native app is running and currently shows a Flutter screen.
- The app was built in debug mode.
- The module directory is the one used by the host project.
- The device or simulator is reachable through
adbor Xcode. - The native app has not been terminated and relaunched in a different process.
- The Flutter SDK used by the module matches the SDK expected by the host integration.
If hot reload reports that the connection was lost, the engine may have been destroyed when the Flutter view closed. Reopen the Flutter screen and run flutter attach again. A cached engine can remain alive after a view closes, but that behavior depends on the host's lifecycle code.
Native breakpoints and Dart breakpoints are independent. Set Dart breakpoints in the module's source files, then attach the debugger. Set Kotlin, Java, Swift, or Objective-C breakpoints in Android Studio or Xcode. When a platform channel call crosses the boundary, inspect both sides: verify the Dart arguments, the native method name, and the returned value.
Exercise
You changed a Dart widget, but hot reload does nothing and flutter attach cannot find an application. What should you check first?
Show answer
Confirm that the native host is running in debug mode, that a Flutter screen is visible, and that the terminal is in the module directory used by that host.