1 flutter protocol_error ai_generated true

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

ID: flutter/platform-channel-argument-error

Also available as: JSON · Markdown · 中文
85%Fix Rate
88%Confidence
1Evidence
2024-03-15First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
Flutter 3.22.0 active
Dart 3.4.0 active
Kotlin 1.9.22 active
Swift 5.9 active

Root Cause

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

generic

中文

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

Official Documentation

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

Workarounds

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

Dead Ends

Common approaches that don't work:

  1. 95% fail

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

  2. 90% fail

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

  3. 80% fail

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