This is my api calling logic.
using this function in getx Controller. Calling a get API.
I am parsing the "Accommodation" Model in my function and seem to get an empty array. I can assure the api is being called without any network issues
accommodationUpdateOrCreationDecision() async {
List<Accommodation> accommdationModelsList = [];
try {
var headers = {
"Authorization":
"..u3nHlV7nZ5-BlGfoBb91of_RZ4W-Qn_tJOUwremWUA",
};
acTitleController.value.text = "hello world";
var response = await _apiServices.getReq(
endPoint: '${StringConstants.apiBaseUrl}/host/accommodation',
headers: headers,
);
if (response.statusCode == 400) {
log("Error Code ==> ${response.statusCode}");
}
log("Status Code ==> ${response.statusCode}");
var accommDataModel = ApiResponseModel.fromJson(
response.data,
);
if (accommDataModel.success == true) {
accommadateUpdateDeleteBool.value = true;
for (var data in response.data["data"]["accommodations"]) {
Accommodation model = Accommodation.fromJson(data);
accommdationModelsList.add(model);
}
log("response Model : ${accommdationModelsList}");
} else {
accommadateUpdateDeleteBool.value = false;
}
log("response Model success: ${accommDataModel.success.toString()}");
} catch (e) {
log("error : ${e.toString()}");
}
}
This is what my terminal says
[log] Status Code ==> 200
[log] response Model : []
[log] response Model success: true
There shouldn’t be an empty array. Chatgpt cannot fix this.
The problematic part of the code is
for (var data in response.data["data"]["accommodations"]) {
Accommodation model = Accommodation.fromJson(data);
accommdationModelsList.add(model);
}
There should have been an array full of this json response
{
"success": true,
"data": {
"message": "Accommodation fetched successfully",
"baseUrl": "http://64.226.125.111/rev-be/uploads/",
"accommodations": [
{
"location": {
"type": "Point",
"coordinates": [
67,
24
]
},
"status": "Available",
"_id": "64fc661abdd6042a77392b85",
"host": "64e762462bbed117abce4cac",
"title": "Updated Riverside Retreat",
"desc": "Relax by the river at our tranquil retreat.",
"images": [
"1695389483120-card-img-logo.png"
],
"roomPrice": 2999,
"dinnerPrice": 699,
"roomCapacity": 12,
"dinnerCapacity": 9,
"services": [],
"isActive": true,
"createdAt": "2023-09-09T12:33:30.047Z",
"updatedAt": "2023-09-22T13:31:23.121Z",
"__v": 0,
"reviewsId": [
"6501bc9189fcd2743e9d0e28",
"6501bca689fcd2743e9d0e2f"
],
"rating": 3.7
}
]
}
}
This is the Model im parsing.
class Accommodation {
final String? id;
final String? host;
final String? title;
final String? desc;
final List<String>? images;
final int? roomPrice;
final int? dinnerPrice;
final Location? location;
final int? roomCapacity;
final int? dinnerCapacity;
final List<dynamic>? services;
final bool? isActive;
final DateTime? createdAt;
final DateTime? updatedAt;
final int? v;
final List<dynamic>? bookings;
Accommodation({
this.id,
this.host,
this.title,
this.desc,
this.images,
this.roomPrice,
this.dinnerPrice,
this.location,
this.roomCapacity,
this.dinnerCapacity,
this.services,
this.isActive,
this.createdAt,
this.updatedAt,
this.v,
this.bookings,
});
factory Accommodation.fromJson(Map<String, dynamic> json) => Accommodation(
id: json["_id"],
host: json["host"],
title: json["title"],
desc: json["desc"],
images: List<String>.from(json["images"].map((x) => "${StringConstants.imageBaseUrl}$x")),
roomPrice: json["roomPrice"],
dinnerPrice: json["dinnerPrice"],
location: Location.fromJson(json["location"]),
roomCapacity: json["roomCapacity"],
dinnerCapacity: json["dinnerCapacity"],
services: List<dynamic>.from(json["services"].map((x) => x)),
isActive: json["isActive"],
createdAt: DateTime.parse(json["createdAt"]),
updatedAt: DateTime.parse(json["updatedAt"]),
v: json["__v"],
bookings: json["bookings"] == null
? []
: List<dynamic>.from(json["bookings"]!.map((x) => x)),
);
Map<String, dynamic> toJson() => {
"_id": id,
"host": host,
"title": title,
"desc": desc,
"images": List<dynamic>.from(images!.map((x) => x)),
"roomPrice": roomPrice,
"dinnerPrice": dinnerPrice,
"location": location!.toJson(),
"roomCapacity": roomCapacity,
"dinnerCapacity": dinnerCapacity,
"services": List<dynamic>.from(services!.map((x) => x)),
"isActive": isActive,
"createdAt": createdAt!.toIso8601String(),
"updatedAt": updatedAt!.toIso8601String(),
"__v": v,
"bookings":
bookings == null ? [] : List<dynamic>.from(bookings!.map((x) => x)),
};
}
class Location {
final String? type;
final List<double>? coordinates;
Location({
this.type,
this.coordinates,
});
factory Location.fromJson(Map<String, dynamic> json) => Location(
type: json["type"],
coordinates:
List<double>.from(json["coordinates"].map((x) => x?.toDouble())),
);
Map<String, dynamic> toJson() => {
"type": type,
"coordinates": List<dynamic>.from(coordinates!.map((x) => x)),
};
}
2
Answers
The issue was solved itself today. (The very next day of posting the question. ) Thanks for the help.
Try this: