Flutter报错:Unhandled Exception: type ‘_Map<String, dynamic>‘ is not a subtype of type ‘String‘

这个是因为我们在使用 jsondecode() 函数时,误把_Map<String, dynamic>类型带入jsondecode函数里面。

jsondecode 函数本身就是让json字符串解码为_Map<String, dynamic>类型json对象。

然后接着就可以使用forEach来遍历json对象的键值对。

比如 jsonMap就是一个_Map<String, dynamic>类型:

jsonMap.forEach((key, value) {
    print('Key: $key');
    print('Value: $value');
    print('------');
  });

或者使用迭代器遍历:

var iterator = jsonMap.entries.iterator;
  while (iterator.moveNext()) {
    var entry = iterator.current;
    print('Key: ${entry.key}');
    print('Value: ${entry.value}');
    print('------');
  }

猜你喜欢

转载自blog.csdn.net/DongShanYuXiao/article/details/132115708
今日推荐