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.
 
 
 
 
 
 

87 lines
2.4 KiB

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