returnSingleValue<Z> method

Future<Z> returnSingleValue<Z>()

Returns a unique single value from the API.

Sends a GET request to the API and expects a single value in response. The type of the returned value will be T, which can be any type like bool, int, or String.

Usage Example

.returnSingleValue()

The result will be a single value like: true, 1, or 'string'.

Implementation

Future<Z> returnSingleValue<Z>() async {
  return _fetchData<Z>(
    parseUrl: _parseUrl,
    processResponse: (cachedData) {
     // First decode the JSON if it's a valid JSON string
      var decodedData = jsonDecode(cachedData);

      // Check if the generic type Z is bool and if decodedData is a bool
      if (Z == bool && decodedData is bool) {
        return decodedData as Z;
      }

      // Check if the generic type Z is int and if decodedData is a number
      if (Z == int && decodedData is num) {
        return decodedData.toInt() as Z;
      }

      // Check if the generic type Z is String and if decodedData is a string
      if (Z == String && decodedData is String) {
        return decodedData as Z;
      }

      // If it's not possible to convert, throw an exception
      throw Exception('Incompatible return type');
    },
  ) as Future<Z>;
}