get method
Future<BaseResponseModel>
get(
- String apiKey,
- BuildContext context, {
- String? apiUrl,
- dynamic bodyData,
- Options? options,
get method will be used to call GET
method in all the methods which is making use of GET
methods.
apiUrl
will be used to specify the particular url that will be used for the API call.
options
will be the GET
request Options
given by the Dio
package.
Implementation
Future<BaseResponseModel> get(String apiKey, BuildContext context,
{String? apiUrl, dynamic bodyData, Options? options}) async {
/// Processing Basic Auth according to apiKey passed.
String basicAuth =
'Basic ${base64.encode(utf8.encode('$apiKey:$password'))}';
/// Passing the basic auth in the "Authorization" header of the Dio request.
dio.options.headers["Authorization"] = basicAuth;
try {
// Calling Dio's get request by passing the apiUrl and options received in parameter.
final apiResponse = await dio.get(apiUrl ?? "", options: options);
if (apiResponse.statusCode == 200) {
// Storing the api response in a variable "response" and then decoding it in Json format.
var response = apiResponse.data;
var jsonData = jsonDecode(jsonEncode(response));
// Assigning relevant data in the constructor and returning the json type response value from here.
return BaseResponseModel(
result: jsonData, success: true, message: "Success");
} else {
return BaseResponseModel(
result: null, success: false, message: "Success");
}
} catch (err) {
if (err is DioException) {
/// Showing SnackBar when there is any API exception takes place.
if (context.mounted) {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text(
"${err.response?.statusMessage} [${err.response?.statusCode}]")));
}
/// Return a null constructor with respective error message and false success flag.
return BaseResponseModel(
result: null, success: false, message: err.response?.statusMessage);
} else {
/// Showing SnackBar when there is some other exception which is not related to API request.
if (context.mounted) {
ScaffoldMessenger.of(context)
.showSnackBar(SnackBar(content: Text("Something went wrong!")));
}
/// Return a null constructor with respective error message and false success flag.
return BaseResponseModel(
result: null, success: false, message: "Something went wrong!");
}
}
}