如何修复Ffltter和Swift之间有关数组和字典的消息通道的强制转换错误

46scxncf  于 2022-10-04  发布在  Swift
关注(0)|答案(1)|浏览(116)

在一个定制插件中,我有一些SWIFT代码,我想返回一组字典,如下所示:

func myCustomFunc(call: FlutterMethodCall, result: @escaping FlutterResult) {
  var someArrayOfDicts: [[String: Int]] = []
  someArrayOfDicts.append(["fieldA": 12345, "fieldB": 67890])
  result(someArrayOfDicts)
}

但是当我.invokeMethod<List<Map<String, int>>>('myCustomFunc')时,我得到一个错误:_CastError (type 'List<Object>' is not a subtype of type 'List<Map<String, int>>' in type cast)

有关报文传送编解码器的颤动频道文档似乎表明,SWIFT中的ArrayDictionary在DART中被接收为ListMap

l7wslrjt

l7wslrjt1#

根据您的报告:

但是当我.invokeMethod<List<Map<String, int>>>('myCustomFunc')时,我得到一个错误:_CastError (type 'List<Object>' is not a subtype of type 'List<Map<String, int>>' in type cast)

我建议这样做:

.invokeMethod<List>('myCustomFunc')

因为您已经知道定义了哪些数据类型。您可以尝试从结果列表中提取数据,然后将其设置为所需的格式。

例如:

List<Map<String,int>> x =[];
_listFromInvoke.forEach((element){
 print(element); //print for eg. element.key or element['fieldA'] (Basically trial n error)
});

相关问题