breaking exception '_InternalLinkedHashMap<dynamic, dynamic>' is not a subtype of type 'Map<String, String>'

匿名 (未验证) 提交于 2019-12-03 01:38:01

问题:

I ran flutter upgrade today...

I'm now on v0.2.11 and I'm getting a strange runtime error in this function:

Future apiCall([Map params = const {}]) async {   loading = true;   Map stringParams = {};   params.forEach((k,v)=>stringParams[k.toString()] = v.toString());   Uri url = new Uri.https(apiDomain, apiPath, stringParams);   print(url);   var result = await http.post(     url,     body: {'apikey': apiKey}   );   loading = false;   print(result.body);   return json.decode(result.body); } 

I'm calling the function without any params and I get the subtype error.

This code works in dartpad.

Does anyone have an idea what might be going on?

回答1:

The constructor for Uri.https requires a Map with a runtime type of Map<String, String>. When you create stringParams without any type annotations, you are actually creating a Map<dynamic, dynamic>. The correct way to create this for Dart 2 is

Map<String, String> stringParams = {}; // or var stringParams = <String, String>{}; 

The reason this used to work is that in Dart 1, even in strong mode, dynamic was fuzzy and acted like both Object and null - meaning a dynamic type was assignable to and from anything. In Dart 2, dynamic acts just like Object, except you can call methods or access properties on it without a downcast.



回答2:

I used this

    if(json["key"]!= null){        this.active_guests = json["key"].cast<String, int>();      } 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!