returnSingleObjectOf method
Fetches and converts a single object from the API.
This method sends a GET request to the specified url
, and expects a JSON
response that it converts to an object of type T
using the provided
fromJson
function.
The fromJson
function should take a Map<String, dynamic> as its only
parameter and return an instance of T
. It is used to convert the JSON
response from the API into an object.
Parameters:
fromJson
: A function that deserializes the JSON response to an object of typeT
.
Returns:
- A Future that completes with an object of type
T
.
Throws:
- ErrorResponseException if the server returns a 401 Unauthorized response, or if the server returns a non-200 status code.
Example:
final object = await returnSingleObjectOf((json) => MyModel.fromJson(json));
Note:
- Ensure that the
fromJson
function correctly handles the deserialization of the JSON to the desired object.
Implementation
Future<T> returnSingleObjectOf(T Function(Map<String, dynamic>) fromJson){
return _fetchData<T>(
parseUrl: _parseUrl,
processResponse: (cachedData) {
return fromJson(cachedData.isEmpty ? { 'id': 0 } : jsonDecode(cachedData));
},
);
}