【发布时间】:2019-07-06 18:35:08
【问题描述】:
我的代码:
import 'dart:convert';
String jsonString = '''{
"a": "g",
"b": "h",
"c": "j",
"d": "k"
}''';
Map<String, String> map = json.decode(jsonString);
得到错误:
Unhandled exception:
type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'Map<String, String>'
#0 main (file:///...)
#1 _startIsolate.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:298:32)
#2 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:171:12)
Map<String, dynamic> map ...
和Map<String, Object> map ...
正在工作。
我最终尝试为不同的 JSON 反序列化 Map<String, Map>,它给了我同样的错误,所以我决定首先使用基本的 Map<String, String>,以更好地了解我做错了什么
【问题讨论】:
-
试试
Map<String, String> data = Map.castFrom(json.decode(jsonString))。如果这仍然失败,请检查您的 JSON 数据中的所有值是否都是字符串类型 -
这是有效的,你能把这个作为答案发布吗?