问题情况

本来按照书上的思路都封装好了Dio,半自动生成了Json Model,结果在实际应用上出现了以下异常:

Unhandled Exception: type 'String' is not a subtype of type 'Map<String, dynamic>'

该异常指向了我进行Json Model化的代码:(User为自己编写的Json Model类)

Response res = await dio.get<String>(...);
return User.fromJson(res.data); // 异常指向了这一行

分析原因:

初步分析是User.fromJson()需要传入Map而res.data的数据类型是String,但是用User.fromJson(res.data as Map<String, dynamic>)这样的写法依然不行。

后来翻书找到了原因,在JSON序列化之前需要用json.decode(json)将String转换为Map,例如:

Map userMap = json.decode(json);
var user = new User.fromJson(userMap);

解决方法

对res.data进行jsonDecode,原代码改成以下代码解决:

return Recommender.fromJson(jsonDecode(res.data));

后续发现的真问题所在和避免方法

后续写的代码发现dio.get(...)不用泛型指明返回类型也会报错。

该异常情况主要是与dio.get方法的返回值有关,如果是dio.get(...)这样使用的话它可能返回String也可能返回Map,所以建议指定泛型来避免异常,例如指定返回String则这样使用:dio.get<String>(...),实际使用代码如下:

Response res = await dio.get("url");
return User.fromJson(jsonDecode(res.data));

注:

本文所使用的环境和依赖:

environment:sdk: ">=2.12.0 <3.0.0"
dependencies:...dio: ^4.0.0json_annotation: ^4.0.1
dev_dependencies:...build_runner: ^2.0.1json_serializable: ^4.1.1

Flutter调用JSON序列化出现type ‘String‘ is not a subtype of type ‘MapString, dynamic‘相关推荐

  1. flutter type ‘int‘ is not a subtype of type ‘String?‘

    type 'int' is not a subtype of type 'String?' 不是int,将int改成String类型 Unhandled Exception: type 'double ...

  2. Type ‘string | null‘ is not assignable to type ‘string‘. Type ‘null‘ is not assignable to type ‘

    报错如下: Type 'string | null' is not assignable to type 'string'. Type 'null' is not assignable to type ...

  3. 【flutter】type ‘Null‘ is not a subtype of type ‘bool‘

    type 'Null' is not a subtype of type 'bool' 类型不对 解决方法 判断返回的类型是否正确 print('类型:${data['isDayTime'].runt ...

  4. Dart或Flutter中解决异常-type ‘int‘ is not a subtype of type ‘double‘

    文章目录 出现场景 解决方案 声明num(推荐) 判断类型 出现场景 服务端返回的金额数据可能是整数,也可能是小数. 无论我们按int或double来解析,都可能出错. 如果我们定义的类型是int,返 ...

  5. Flutter-解决Try catch出现异常:type ‘_TypeError‘ is not a subtype of type ‘Exception‘ in type cast

    文章目录 出现场景 解决方案 分开处理(推荐) 判断类型 出现场景 使用Dio时,网络请求可能会出现异常,需要用try catch捕获. 捕获代码如下 try {var response = awai ...

  6. flutter:I/flutter ( 5290): type ‘Null‘ is not a subtype of type ‘String‘

    在flutter项目中遇到了如上报错,该错误并不像普通bug那样终止运行,所以找了很久才找到原因,在此记录一下. 原因:json数据从后端发送过来,转换的时候由于数据类型不一样造成的原因. 错误地方: ...

  7. flutter int.parse报错type ‘int‘ is not a subtype of type ‘String‘

    项目中想要计算一些通过接口获取的数值,便使用int.parse('')转换. 尝试打印o['num']发现也有值,只是看不到类型,int.parse(arg)的参数是string类型,尝试对o['nu ...

  8. Value of type “string“ is not suitable for value type “Numeric (float)“. Value “Traceback (most rece

    解决办法:zabbix监控项中修改字符类型

  9. 将$type添加到System.Text.Json序列化中,就像Newtonsoft那样用于动态对象属性

    目录 介绍 用户故事5:在System.Text.Json JsonSerializer中支持动态类型 演示项目和测试 修改模型方法 包装方法 总结 Pro Coders团队最近将一个大型项目从New ...

  10. php json字符串序列化,JSON序列化与反序列化实现方法(附代码)

    这次给大家带来JSON序列化与反序列化实现方法(附代码),JSON序列化与反序列化实现的注意事项有哪些,下面就是实战案例,一起来看一下. 一.JSON简介 JSON(JavaScript Object ...

最新文章

  1. android Merger 代替 FrameLayout:布局优化
  2. 网狐棋牌客户端连接服务器修改方法6,网狐游戏服务器 客户端连接
  3. wordexcelppt文档加密方式
  4. Linux下常用日志分析工具
  5. odoo13中的模型类(模型属性、字段属性、字段类型)
  6. Android顶部粘至视图具体解释
  7. python double free_python错误:double free或corruption(out):0x0000000001e4b030
  8. 作者:熊森林(1988-),男,中国科学院国家空间科学中心助理研究员
  9. 程序迭代时测试操作的要点(后端前端)
  10. Shell循环输入符合条件为止
  11. 数字化工厂的五大系统_工厂通过MES系统对车间设备数字化管理
  12. [基础篇]PHP引用变量
  13. 学生成绩管理系统 需求规格说明书
  14. c++实现计算二十四点--zj
  15. 移动硬盘提示“需要格式化”
  16. 数据类型--Number类型
  17. android 短信字体,安卓短信字体 安卓短信字体大小设置
  18. 从程序员到项目经理(25):对绩效考核的吐槽
  19. 微信QQ支付宝三合一收款二维码实现原理
  20. thinkpad联想IBM笔记本为什么出现fan error

热门文章

  1. 防火墙结构之屏蔽主机体系结构
  2. 斗牛/牛牛经典算法java版
  3. 实现网站在线客服系统代码过程中解决问题-js实现点击复制文本
  4. 读写锁就是恶霸和良民一起桑拿
  5. Gazebo models / Gazebo Error [Node.cc:90] No namespace found
  6. 【美学集】色彩之冷暖色
  7. Mac m1安装jmeter
  8. 电子电工技术------电阻元件,电感元件与电容元件
  9. 11年艺术学习“转投”数学,他出版首本TensorFlow中文教材,成为蚂蚁金服技术大军一员...
  10. 算法学习 | 期望dp+概率dp