【发布时间】:2021-11-28 17:46:08
【问题描述】:
我是 Flutter 的新手,我正在尝试获取在我执行发布请求时返回的响应。 这是我尝试过的,但它没有返回 ReservationResponse 对象,而是返回此消息 "Instance of ReservationResponse" 。我可能做错了什么,我该如何纠正?
Future < dynamic > priceReservation(priceReservation) async {
var content = jsonEncode(priceReservation.toJson());
const baseUrl = ApiEndPoint.baseUrl;
searchUrl = '$baseUrl/reservation/price';
var response = await http.post(
Uri.parse(searchUrl),
body: content,
headers: {
"Content-Type": "application/json"
},
);
final data = json.decode(response.body);
ReservationResponse responseObject;
if (response.statusCode == 200) {
responseObject = ReservationResponse.fromJson(data);
// print(data);
print(responseObject); // it returns an "Instance of the ReservationResponse" object instead of the actual response
return responseObject;
} else
return null;
}
// My Class looks like this
@JsonSerializable()
class ReservationResponse {
String ? id;
String ? email;
int ? quantity;
int ? nights;
double ? totalPricePerRoomPerNight;
TotalPrice ? totalPrice;
Room ? room;
DateTime ? checkInDate;
DateTime ? checkOutDate;
List ? taxes = [];
List ? discounts = [];
ReservationResponse({
this.id,
this.email,
this.quantity,
this.nights,
this.totalPricePerRoomPerNight,
this.totalPrice,
this.room,
this.checkInDate,
this.checkOutDate,
this.taxes,
this.discounts,
});
factory ReservationResponse.fromJson(Map < String, dynamic > json) =>
_$ReservationResponseFromJson(json);
Map < String, dynamic > toJson() => _$ReservationResponseToJson(this);
}
【问题讨论】:
标签: flutter dart flutter-web