I have a problem with my dart object. First I recieve data from my http api, which is a json, than I use the jsonDecode function and than I try to work with it, but it seems to be impossible.
This is the json:
[{car_name: virtual_vehicle, company_name: company1}, {car_name: virtual_vehicle_2, company_name: company2}, {car_name: virtual_vehicle_3, company_name: company3}]
This is the fetch function:
Future<List?> availableCars({
bool? wait,
int? since,
}) async {
final response = await availableCarsWithHttpInfo(
wait: wait,
since: since,
);
if (response.statusCode >= HttpStatus.badRequest) {
throw ApiException(response.statusCode, await _decodeBodyBytes(response));
}
// When a remote server returns no body with a status of 204, we shall not decode it.
// At the time of writing this, `dart:convert` will throw an "Unexpected end of input"
// FormatException when trying to decode an empty string.
if (response.body.isNotEmpty &&
response.statusCode != HttpStatus.noContent) {
final responseBody = await _decodeBodyBytes(response);
//return (await apiClient.deserializeAsync(responseBody, 'List<Car>') as List).cast<Car>().toList(growable: false);
return jsonDecode(responseBody);
}
return null;
}
And this is the function where i try to format the data from the api fetch:
Future<void> fetchCarsandCompanies() async {
var apiInstance = CarApi(globals.apiClient);
try {
var cars = await apiInstance.availableCars();
print(cars);
if (cars != Null) {
globals.carList = cars;
globals.companyList = cars?.map((car) => car['companyName']);
}
} catch (error) {
print('Fetch cars and companies error: $error');
}
}
If anybody would have any idea why it does not work, please let me know. Thank you.
0