I am using Dio and retrofit to make Http requests and I have a problem with the model that the API returns.
Previously I was using Flutter's own Http and parsing the response to fit what I needed.
The model I have is the following:
import 'package:json_annotation/json_annotation.dart';
part 'price_model.g.dart';
@JsonSerializable()
class PriceModel {
String? date;
String? hour;
bool? isCheap;
bool? isUnderAvg;
String? market;
double? price;
String? units;
PriceModel(
{this.date,
this.hour,
this.isCheap,
this.isUnderAvg,
this.market,
this.price,
this.units});
factory PriceModel.fromJson(Map<String, dynamic> json) => _$PriceModelFromJson(json);
Map<String, dynamic> toJson() => _$PriceModelToJson(this);
}
My Retrofit class is as follows:
import 'package:electricity_price/app/home/models/price_model.dart';
import 'dart:async';
import 'package:dio/dio.dart';
import 'package:retrofit/http.dart';
part "price_repository.g.dart";
@RestApi()
abstract class PriceRepository {
factory PriceRepository(Dio dio, {String baseUrl}) = _PriceRepository;
@GET('all?zone=PCB')
Future<List<PriceModel>> getPrices();
}
But the API response comes in this format:
{
"00-01": {
"date": "22-09-2022",
"hour": "00-01",
"is-cheap": true,
"is-under-avg": true,
"market": "PVPC",
"price": 357.04,
"units": "€/Mwh"
},
"01-02": {
"date": "22-09-2022",
"hour": "01-02",
"is-cheap": false,
"is-under-avg": true,
"market": "PVPC",
"price": 377.26,
"units": "€/Mwh"
}
The result of this is the following error:
DioError [DioErrorType.other]: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'List?' in type cast
How can I model this or what else can I do to be able to parse the response?
Thanks in advance