import 'dart:io'; import 'package:dio/dio.dart'; abstract class BaseRepository { final Dio dio; BaseRepository({required this.dio}); Future get({ required String service, CancelToken? cancelToken, }) async { try { final response = await dio.get( service, cancelToken: cancelToken, options: Options( headers: { HttpHeaders.contentTypeHeader: "application/json", }, contentType: "application/json", ), ); if (response.statusCode != 200) { throw BaseRepositoryException( message: "Invalid Http Response ${response.statusCode}", ); } return response.data; } on DioError catch (e) { throw BaseRepositoryException(message: e.message); } on SocketException catch (e) { throw BaseRepositoryException(message: e.message); } on BaseRepositoryException catch (e) { throw BaseRepositoryException(message: e.message); } } Future post({ required String service, required Map jsonParam, CancelToken? cancelToken, }) async { try { final response = await dio.post( service, data: jsonParam, cancelToken: cancelToken, options: Options( headers: { HttpHeaders.contentTypeHeader: "application/json", }, contentType: "application/json", ), ); if (response.statusCode != 200) { throw BaseRepositoryException( message: "Invalid Http Response ${response.statusCode}", ); } return response.data; } on DioError catch (e) { throw BaseRepositoryException(message: e.message); } on SocketException catch (e) { throw BaseRepositoryException(message: e.message); } on BaseRepositoryException catch (e) { throw BaseRepositoryException(message: e.message); } } } class BaseRepositoryException implements Exception { final String message; BaseRepositoryException({ required this.message, }); }