获取 _InternalLinkedHashMap<String, dynamic>' 不是 'FutureOr<Response>' 类型的子类型
Posted
技术标签:
【中文标题】获取 _InternalLinkedHashMap<String, dynamic>\' 不是 \'FutureOr<Response>\' 类型的子类型【英文标题】:Getting _InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'FutureOr<Response>'获取 _InternalLinkedHashMap<String, dynamic>' 不是 'FutureOr<Response>' 类型的子类型 【发布时间】:2019-05-08 03:26:47 【问题描述】:我是 Dart/Flutter 的新手,我正在尝试获取使用 web 服务的未来的结果:
Future<Map> _readData() async
(...)
http.Response response = await http.post(
request,
headers:
"SOAPAction": "http://www.totvs.com/IwsConsultaSQL/RealizarConsultaSQL",
"Content-Type": "text/xml;charset=UTF-8",
"Authorization": "Basic bWVzdHJlOnRvdHZz",
"cache-control": "no-cache"
,
body: utf8.encode(requestBody),
encoding: Encoding.getByName("UTF-8")
).then((onValue)
(...)
var dataFinal = jsonDecode(jsondataFinal);
return dataFinal ;
);
在我的初始化状态下我调用了这个函数,所以我可以使用这个结果来构建我的布局:
@override
void initState ()
super.initState();
_readData().then((data)
setState(()
print(data);
);
);
我总是遇到这个异常:
_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'FutureOr<Response>'
对我做错了什么有任何想法吗?
谢谢
【问题讨论】:
您不必同时使用await
和then
。
好的,但这不是问题:D,我会调整它,但即使没有等待它也会给我同样的错误。
【参考方案1】:
此代码是否出现此错误?
Future<Map> _readData() async
//...
http.Response response = await http.post(
request,
headers:
"SOAPAction": "http://www.totvs.com/IwsConsultaSQL/RealizarConsultaSQL",
"Content-Type": "text/xml;charset=UTF-8",
"Authorization": "Basic bWVzdHJlOnRvdHZz",
"cache-control": "no-cache"
,
body: utf8.encode(requestBody),
encoding: Encoding.getByName("UTF-8")
);
//...
var dataFinal = jsonDecode(jsondataFinal);
return dataFinal ;
【讨论】:
【参考方案2】:您不能同时使用 await 和 then。您的代码应如下所示
Future<Map> _readData() async
(...)
http.Response response = await http.post(
request,
headers:
"SOAPAction": "http://www.totvs.com/IwsConsultaSQL/RealizarConsultaSQL",
"Content-Type": "text/xml;charset=UTF-8",
"Authorization": "Basic bWVzdHJlOnRvdHZz",
"cache-control": "no-cache"
,
body: utf8.encode(requestBody),
encoding: Encoding.getByName("UTF-8")
);
var dataFinal = jsonDecode(response.body);
return dataFinal ;
【讨论】:
以上是关于获取 _InternalLinkedHashMap<String, dynamic>' 不是 'FutureOr<Response>' 类型的子类型的主要内容,如果未能解决你的问题,请参考以下文章
颤动的json未处理的异常:类型'_InternalLinkedHashMap
错误:类型 '_InternalLinkedHashMap<String, dynamic>' 不是类型 'List<dynamic>' 的子类型
NoSuchMethodError:类 '_InternalLinkedHashMap<String, dynamic>' 没有具有匹配参数的实例方法 'cast'
新:类型“_InternalLinkedHashMap<String, dynamic>”不是“DocumentSnapshot”类型的子类型
JsonSerializable - fromJson 在嵌套对象上抛出 _InternalLinkedHashMap<dynamic, dynamic> 异常
NoSuchMethodError:类 '_InternalLinkedHashMap<String, dynamic>' 没有具有匹配参数的实例方法 'map'。 (扑)