One UI in flutter
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

88 lines
2.4 KiB

2 years ago
import 'dart:convert';
2 years ago
import 'dart:io';
import 'package:dio/dio.dart';
abstract class BaseRepository {
final Dio dio;
BaseRepository({required this.dio});
2 years ago
Future<Map<String, dynamic>> get({
2 years ago
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}",
);
}
2 years ago
final jData = jsonDecode(response.data);
if (jData["status"] == "ERR") {
throw BaseRepositoryException(message: jData["message"]);
}
return jData;
2 years ago
} 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);
}
}
2 years ago
Future<Map<String, dynamic>> post({
2 years ago
required String service,
required Map<String, dynamic> 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(
2 years ago
message:
"Invalid Http Response ${response.statusCode} |${response.statusMessage}",
2 years ago
);
}
2 years ago
final jData = jsonDecode(response.data);
if (jData["status"] == "ERR") {
throw BaseRepositoryException(message: jData["message"]);
}
return jData;
2 years ago
} 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,
});
}