打破异常'_InternalLinkedHashMap< dynamic,dynamic>'不是'Map< String,String>'类型的子类型 [英] breaking exception '_InternalLinkedHashMap<dynamic, dynamic>' is not a subtype of type 'Map<String, String>'
问题描述
我今天进行了全面升级...
I ran flutter upgrade today...
我现在使用的是v0.2.11,并且在此函数中遇到了奇怪的运行时错误:
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.
此代码在dartpad中有效。
This code works in dartpad.
有人知道会发生什么吗?
Does anyone have an idea what might be going on?
推荐答案
Uri。https
要求Map的运行时类型为 Map< String,String>
。创建没有任何类型注释的 stringParams
时,实际上是在创建 Map< dynamic,dynamic>
。为Dart 2创建此代码的正确方法是
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>{};
之所以起作用,是因为在Dart 1中,即使在强模式下, dynamic
很模糊,表现得像 Object
和 null
一样-表示动态类型可以分配给任何东西。在Dart 2中, dynamic
的行为与 Object
相似,不同之处在于您可以调用方法或访问其上的属性而不会出现下降。
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.
这篇关于打破异常'_InternalLinkedHashMap< dynamic,dynamic>'不是'Map< String,String>'类型的子类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!