【发布时间】:2019-01-14 22:00:46
【问题描述】:
我尝试在对我的Map<String, String> 变量进行 json 编码然后对其进行 json 解码之后为其分配一个值。以下代码示例是简化的重现;
import 'dart:convert';
String toJson(dynamic object) {
var encoder = new JsonEncoder.withIndent(" ");
return encoder.convert(object);
}
dynamic fromJson(String jsonString) {
return json.decode(jsonString);
}
void main() {
Map<String, String> data = {"hello": "world"};
String jsonString = toJson(data);
data = fromJson(jsonString);
print(data);
}
当我运行它时,它失败了;
Unhandled exception:
type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'Map<String, String>'
而on this online editor 失败并出现不同的错误;
Uncaught exception:
TypeError: Instance of '_JsonMap': type '_JsonMap' is not a subtype of type 'Map<String, String>'
【问题讨论】: