Closed yfming93 closed 4 months ago
模型类截图下
模型类截图下
之前使用5.0.9版本是正常的,从5.0.9直接升级到5.1.5不正常。
方便给下json和模型类都发我吗 可以加我qq 然后私聊给我
方便给下json和模型类都发我吗 可以加我qq 然后私聊给我 sorry。我先自己看看。我这边是显示正常再次交互的不正常。我怀疑是我自己这边的原因。我先确认下
好的 如果发现问题了 或者 解决 麻烦给反馈下 方便下次其他朋友们遇到这个问题快速解决
A//最新版 return convertFuncMap[type]!(value as Map<String, dynamic>) as T; B//老版本 return convertFuncMap[type]!(Map<String, dynamic>.from(value)) as T; 两个版本第一次使用fromJson展示都正常。A// 版本把实例toJson()后得到的JSON再去fromJson就报错如上。B// 版本这些情况都不报错。
toJson()后得到的JSON再去fromJson报错。 测试的JSON:
{
"id": 57539,
"path": [
{
"uid": "files/20230907/1694069454950300",
"url": "https://xxx.oss-cn-beijing.aliyuncs.com/files/20230907/1694069454950300.jpeg",
"name": "files/20230907/1694069454950300.jpeg",
"size": 129886,
"type": "jpeg",
"thumbUrl": "https://xxx-beijing.aliyuncs.com/files/20230907/1694069454950300.jpeg"
}
],
"reference": [],
"weather": {
"rh": 50,
"temp": 34,
"text": "晴",
"uptime": "20230907143000",
"wind_dir": "东北风",
"feels_like": 36,
"wind_class": "2级"
},
"status": "assigned",
"publish_status": 20,
"publish_time": "2023-09-07 14:51:30",
"expiry_time": "2023-09-11 23:59:59",
"expiry_status": 40,
"expiry_score": "0.00",
"complaint_expiry_time": "2023-09-11 23:59:59",
"complete_time": null,
"rectify_times": 0,
"remark": null,
"created_at": "2023-09-07 14:51:30",
"updated_at": "2023-09-12 00:00:01",
"patrol_user_name": "xxx",
"top_score_sort_title": "xxx",
"score_sort_title": "xxx",
"evaluate_title": "xxx",
"patrol_depart_name": "ABC",
"belong_depart_name": "xx",
"rectify_depart_name": "xxx",
"sort_name": "xxx",
"road_id": 724,
"tags": []
}
没发现问题啊
是这么吗
正常转换确实无问题,干净无其他业务的demo没有这个问题。我出现这个问题是因为我把toJson后得到的Map<String,dynamic>)数据赋值给百度地图了。然后在业务场景取出使用此时百度地图给我的是Map类型的Json(称为jsn,被强转为Map额不是Map<String,dynamic>))。然后进行转换SdRoadFenceList.fromJson(jsn);此时jsn中的path数组List里面的数据全部是是_Map<Object?, Object?>类型不是Map<String,dynamic>)类型导致转换失败。因为最新转换都是使用return convertFuncMap[type]!(value as Map<String, dynamic>) as T;如果当 value是_Map<Object?, Object?>类型时候 value as Map<String, dynamic>就失败报错了。
不知道新版是因为什么原因把原来的 return convertFuncMap[type]!(Map<String, dynamic>.from(value)) as T;换成在目前这这个方式转换。原来的Map<String, dynamic>.from(value)就适配我说的这个场景不报错。
为了修复这个问题。我对Map做了扩展,把Map类型都转换为Map<String,dynamic>)再去使用你的工具就正常了。 extension MapStrDyn on Map {
Map<String,dynamic> get toStrDynamic { return map((key, value) { if(value.runtimeType.toString().startsWith('List')) return MapEntry(key.toString(), mapLs(value as List)); return MapEntry(key.toString(), value); }); }
List<Map<String,dynamic>> mapLs (List ls) => ls.map((e) => (e as Map).toStrDynamic).toList(); }
最后。如果没有特殊的原因,期望把return convertFuncMap[type]!(value as Map<String, dynamic>) as T; 还是改为原来的 return convertFuncMap[type]!(Map<String, dynamic>.from(value)) as T;
新版本修复了这个问题 更新下就行 最近工作时间是太忙 没有去修复
解决方案就是2种都兼容了,之前反复横跳的原因是场景下需要另外一种,导致反复改这块,这次我做泛型判断
Version
5.1.5
Platforms
dart
Device Model
redmi note9
flutter info
How to reproduce?
asT type '_Map<Object?, Object?>' is not a subtype of type 'Map<String, dynamic>' in type cast #0 JsonConvert._asT
Logs
Example code (optional)
Contact
No response