flutter - Flutter Dio 数据对象
问题描述
我想知道我做的对不对?我通过辅助函数将我的 json_annotation 对象转换为 Map,然后将其发送到 Dio 数据参数
dynamic jsonify(dynamic jsonValue, {bool includeNull = false}) {
final Map<String, dynamic> map = <String, dynamic>{};
try {
if (jsonValue is String &&
json.decode(jsonValue) is Map<String, dynamic>) {
final Map<String, dynamic> tempMap = json.decode(jsonValue);
tempMap.removeWhere(
(String key, dynamic value) => !includeNull && value == null);
tempMap.forEach((String key, dynamic value) {
final dynamic jsonValue = jsonify(value, includeNull: includeNull);
tempMap.update(key, (dynamic existingValue) => jsonValue);
});
map.addAll(tempMap);
} else if (jsonValue is List<dynamic>) {
for (int i = 0; i < jsonValue.length; i++) {
jsonValue[i] = jsonify(jsonValue[i], includeNull: includeNull);
}
return jsonValue;
} else {
return jsonValue;
}
} on FormatException {
return jsonValue;
}
return map; }
我的 json_annotation 对象
@JsonSerializable()
class TripCloseRequest implements Requestable {
/// the constructor to the response of the tours api
TripCloseRequest({this.data});
/// factory constructor for response of api
factory TripCloseRequest.fromJson(Map<String, dynamic> json) =>
_$TripCloseRequestFromJson(json);
/// convert the model to json
String toJson() => json.encode(_$TripCloseRequestToJson(this));
///data - api request data model
@JsonKey(name: 'data')
final TripCloseDetails data;
/// function that converts the request model to form data
@override
Map<String, dynamic> toData() => jsonify(toJson());
}
这是我的 Dio 电话
client.post<dynamic>(_urlCloseTrip, data: tripCloseRequest.toData())
颤振 JSON 注释是否可以替代我的辅助函数 jsonify?
解决方案
推荐阅读
- java - JavaEE 8、Tomcat 8.5、错误状态 404
- php - 函数中的 $_SESSION 变量未传递到下一页
- java - 使用 Okio 的圆形 BufferedSink
- php - 如果使用 PHP 登录,如何允许用户查看 HTML 页面
- java - 2D 空间中的 3D 透视图
- botframework - 由后端每个用户为 Botframework 提供的短语列表
- r - R传单情节船方向
- android - 在最小化应用程序并重新打开时调用 Camera.release() 后正在使用相机
- server - 使用 gsm a6 的 Arduino 程序中的错误主机链接
- actionscript-3 - AS3:创建 BitmapData 的着色副本的最有效方法