I use dio, retrofit to manage api connections. Codes related to annotations and build_runner are generated.
My problem: I receive a list of data in the response. The code that is generated is different from the code that I need. There is a section in the generated code that contains this text.
final _result = await _dio.fetch<Map<String, dynamic>>(_setStreamType<
And the code I need is like this.
final _result = await _dio.fetch<List<dynamic>>(_setStreamType<
This is my code snippet:
@RestApi(baseUrl: BuildConfig.SERVER)
abstract class GetApiService {
factory GetApiService(Dio dio,{String baseUrl}) = _GetApiService;
@GET(MainApi.baseData)
@FormUrlEncoded()
Future<HttpResponse<ResponseList>> getDashboardHomeResponse();
}
Sample my response received:
[
{
'key1':'value1',
'key2':'value2',
'key3':'value3'
}
]
I resoleve problem.
The type of data that you give to the http response must be of the List type. so that your dio response is List<dynamic>.
otherwise , the response generator will return a Map<String, dynamic> type.
My mistake was in passing the data type to HttpResponse.
My class model passing to HttpResponse has a list type variable , but I have to create the class model individually.
This is my edited code snippet:
@RestApi(baseUrl: BuildConfig.SERVER)
abstract class GetApiService {
factory GetApiService(Dio dio,{String baseUrl}) = _GetApiService;
@GET(MainApi.baseData)
@FormUrlEncoded()
Future<HttpResponse<List<ResponseItem>>> getDashboardHomeResponse(); ///Pay attention to the type , Type is List!
}