我如何 return POST 响应对象在 flutter 中?

How can I return the POST response object in flutter?

我是 flutter 的新手,我正在尝试获得在我发出 post 请求时返回的响应。 这是我尝试过的方法,但它没有返回 ReservationResponse 对象,而是 returns 这条消息“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);
}

打印 class 时,您可能应该使用 toString() 方法。您是否在自定义 class 中覆盖了 .toString() 方法?如果没有,请执行此操作


@overide
toString(){
   return 'This is a string of my class. $someData'; //then implement what data the should be returned like this 
}

在您创建的 ReservationResponse class 中,包含上面的代码并输入您要显示的数据。像这样:

@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);

  @override
  toString(){
    String output = 'ReservationResponse: id: ${this.id}, email: 
    ${this.email}'; //and so on for the info you want to return
    return output;
  }
}

我发现我需要 Map 响应,所以我没有将 responseObject class 字符串化,而是像这样返回它的 Map:

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; replace with the map below
  Map < String, dynamic > ? responseObject;
  if (response.statusCode == 200) {
    // responseObject = ReservationResponse.fromJson(data);
    responseObject = data;
    print(responseObject); // returns the Map response
    return responseObject;
  } else
    return null;
}