今天调试公司后台数据接口碰到json数据一直报
[ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: DioError [DioErrorType.DEFAULT]: type 'String' is not a subtype of type 'Map<String, dynamic>'
经过debug调试发现后台响应头 返回的不是contentType: application/json; charset=utf-8
而是 contentType:text/html; charset=utf-8 ,结果就悲剧了,一直以为是自己生成的实体类有问题或者json数据问题,
解决方案1: 后台数据响应返回修改成contentType: application/json; charset=utf-8
方案2:修改retrofit自动生成的代码
@override
Future<DeviceData> getDeviceDetail(deviceCode) async {
ArgumentError.checkNotNull(deviceCode, 'deviceCode');
const _extra = <String, dynamic>{};
final queryParameters = <String, dynamic>{r'device_code': deviceCode};
final _data = <String, dynamic>{};
final _result = await _dio.request<String>(//这里修改成String接收
'manageMachine/getDeviceDetail',
queryParameters: queryParameters,
options: RequestOptions(
method: 'POST',
headers: <String, dynamic>{},
extra: _extra,
baseUrl: baseUrl),
data: _data);
final value = DeviceData.fromJson( json.decode(_result.data.toString() ));//这里将String转json
return value;
}

2465




被折叠的 条评论
为什么被折叠?



