颤振 HTTP 帖子"type 'int' is not a subtype of type 'String' in type cast"



我正在尝试将一些数据发布到此结构的后端:

class Activity {
String id;
String employeeId;
String locationId;
String locationName;
DateTime startDateTime;
DateTime endDateTime;
int breakMinutes;
String actionId;
String actionType;
int startDeviceType;
int endDeviceType;
String comment;
factory Activity.fromJson(Map<String, dynamic> json) =>
_$ActivityFromJson(json);
Map<String, dynamic> toJson() => _$ActivityToJson(this);
}

我正在将类转换为如下所示的地图:

Map<String, dynamic> _$ActivityToJson(Activity instance) => <String, dynamic>{
'id': instance.id,
'employeeId': instance.employeeId,
'locationId': instance.locationId,
'locationName': instance.locationName,
'startDateTime': instance.startDateTime?.toIso8601String(),
'endDateTime': instance.endDateTime?.toIso8601String(),
'breakMinutes': instance.breakMinutes,
'actionId': instance.actionId,
'actionType': instance.actionType,
'startDeviceType': instance.startDeviceType,
'endDeviceType': instance.endDeviceType,
'comment': instance.comment,
};

。并用这些行发送:

Future<HttpError> updateAsync(Activity activity) async {
var url = '...';
...
var body = activity.toJson();
Response response = await http.post(
url,
headers: headers,
body: body,
);
if (isSuccess(response.statusCode)) {
return null;
}
return new HttpError(
errorCode: response.statusCode, errorMessage: response.reasonPhrase);
}

。然后后端想要接收这些数据:

{
"startDateTime": "2019-10-12T19:59:22.801Z",
"endDateTime": "2019-10-12T19:59:22.801Z",
"breakMinutes": 0,
"actionId": "3fa85f64-5717-4562-b3fc-2c963f66afa6",
"startDeviceType": 0,
"endDeviceType": 0,
"comment": "string"
}

我现在面临的问题是调用 post(( 时出现错误

type 'int' is not a subtype of type 'String' in type cast

如果我在转换为 Map 时对整数值调用.toString(),我会得到415 - Unsupported Media Type.

那么这是一个错误还是我真的在这里做错了什么?

更改

var body = activity.toJson(); // here body is still a Map<String, dynamic>

var body = json.encode(activity.toJson()); // here it's a JSON encoded string

相关内容

最新更新