post method

Future<ApiResponse> post(
  1. String url, {
  2. dynamic body,
  3. bool useCache = false,
  4. Duration? ttl,
})

Implementation

Future<ApiResponse> post(String url,
    {dynamic body, bool useCache = false, Duration? ttl}) async {
  final key = _key('POST', url, body);
  if (!ConnectivityService.isConnected) {
    if (useCache) {
      final cached = await CacheManager.getJson(key);
      if (cached != null) return ApiResponse.success(cached);
      return ApiResponse.error('No connection and no cache');
    }
    return ApiResponse.error('No connection');
  }

  try {
    final res = await _dio.post(url, data: body);
    final data = res.data;
    if (useCache) {
      await CacheManager.putJson(key, data, ttl: ttl ?? defaultCacheTtl);
    }
    return ApiResponse.success(data, status: res.statusCode);
  } catch (e) {
    if (useCache) {
      final cached = await CacheManager.getJson(key);
      if (cached != null) return ApiResponse.success(cached);
    }
    return ApiResponse.error(e.toString());
  }
}