1 flutter protocol_error ai_generated true

PlatformException: 错误: 1, 消息: 无效参数, 详情: 期望 Map 但得到 List

PlatformException: error: 1, message: Invalid argument, details: Expected a Map but got List

ID: flutter/platform-channel-argument-error

其他格式: JSON · Markdown 中文 · English
85%修复率
88%置信度
1证据数
2024-03-15首次发现

版本兼容性

版本状态引入弃用备注
Flutter 3.22.0 active
Dart 3.4.0 active
Kotlin 1.9.22 active
Swift 5.9 active

根因分析

平台通道方法调用参数不是有效的 Map,通常是由于原生端编码错误。

English

Platform channel method call arguments are not a valid Map, often due to incorrect encoding on the native side.

generic

官方文档

https://docs.flutter.dev/platform-integration/platform-channels

解决方案

  1. In the native Android handler, ensure arguments are wrapped in a HashMap. Example: `result.success(mapOf("key" to value))` instead of `result.success(listOf(...))`.
  2. On iOS, use a dictionary: `result(["key": value])` and verify the Flutter side expects a Map. Also check for nil values in the dictionary.
  3. Add a try-catch on the native side to log the exact argument type: `try { ... } catch (e) { Log.e("Channel", "Argument type: ${arg.runtimeType}"); }`.

无效尝试

常见但无效的做法:

  1. 95% 失败

    The root cause is in the native method channel implementation, not a transient state issue.

  2. 90% 失败

    The error is raised on the native side before Dart receives the response; null checks don't fix encoding.

  3. 80% 失败

    The error is specific to custom method channel code, not package compatibility.