Flutter MethodChannel嵌套值:'List <dynamic>'不是'FutureOr <List <Map <String,double >>>'类型的子类型

Geo*_*rab 3 dart flutter

我正在编写一个Flutter插件,该插件List<Map<String, double>>从“平台”特定端发送地图列表()。在平台特定方面,我正在使用默认消息编解码器发送这些对象。

// (example: Android side)

List<Map<String, Double>> out = new ArrayList<>();
... fill the map ...
result.success(out);
Run Code Online (Sandbox Code Playgroud)

我在Dart端收到以下这些值:

static Future<List<Map<String, double>>> getListOfMaps() async {
    var traces = await _channel.invokeMethod('getListOfMaps');
    print(traces); // works
    return traces;
}
Run Code Online (Sandbox Code Playgroud)

打印值会给出正确的值。但是,在函数返回上,type 'List<dynamic>' is not a subtype of type 'FutureOr<List<Map<String, double>>>'在运行时出现以下错误,指示从动态值到特定值的转换Map<String, double>不起作用。

如何在Dart中正确转换来自MethodChannels的嵌套值?

Geo*_*rab 5

正如评论中指出的那样,我必须将具有未知运行时类型的每个值分别转换为期望的类型。

static Future<List<Map<String, double>>> getListOfMaps() async {
    List<dynamic> traces = await _channel.invokeMethod(...);
    return traces
        .cast<Map<dynamic, dynamic>>()
        .map((trace) => trace.cast<String, double>())
         .toList();
}
Run Code Online (Sandbox Code Playgroud)