MissingPluginException: 在通道 com.example.app/channel 上等待方法调用结果超时
MissingPluginException: Timeout waiting for method call result on channel com.example.app/channel
ID: flutter/method-channel-timeout
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| Flutter 3.16.0 | active | — | — | — |
| Flutter 3.22.0 | active | — | — | — |
| Dart 3.2.0 | active | — | — | — |
根因分析
从 Dart 到原生端的平台通道方法调用超时,因为原生处理程序从未调用 result.success 或 result.error,通常是由于缺少插件注册或主线程操作阻塞。
English
Platform channel method call from Dart to native side timed out because the native handler never called result.success or result.error, often due to a missing plugin registration or a blocking main thread operation.
官方文档
https://docs.flutter.dev/platform-integration/platform-channels解决方案
-
确保原生插件已注册。在 Android 中,检查 MainActivity.kt/Java 的 configureFlutterEngine 中添加了插件类。例如,在 MainActivity.kt 中: override fun configureFlutterEngine(flutterEngine: FlutterEngine) { super.configureFlutterEngine(flutterEngine) flutterEngine.plugins.add(MyPlugin()) } -
验证原生处理程序始终调用 result.success 或 result.error。在 Android 中,使用 try-finally 块包装方法调用实现以确保调用 result。示例: override fun onMethodCall(call: MethodCall, result: Result) { try { if (call.method == "getData") { result.success(fetchData()) } } catch (e: Exception) { result.error("ERROR", e.message, null) } } -
运行 'flutter clean' 和 'flutter pub get' 重新注册所有插件,然后重新构建应用。
无效尝试
常见但无效的做法:
-
Increase the default timeout duration in Dart code
70% 失败
The timeout is not the root cause; the native side never responds, so extending the timeout only delays the failure.
-
Restart the Flutter app without cleaning the build cache
50% 失败
If the plugin is not registered, restarting without cleaning retains the stale state; a clean build is required.
-
Wrap the method call in a try-catch and ignore the exception
90% 失败
Ignoring the exception suppresses the error but leaves the underlying issue unresolved, leading to silent failures in other features.