Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'package:dio/dio.dart';
- import 'package:sports_asylum/models/location.dart';
- class LocationsApiProvider {
- //the api url
- final String _apiUrl = "http://www.buttermancer.com/api/";
- //initiate the api caller
- Dio _dio;
- //setup the api caller options
- LocationsApiProvider() {
- Options options = Options(receiveTimeout: 5000, connectTimeout: 5000);
- _dio = Dio(options);
- }
- //handle errors if there is an issue with the call
- String _handleError(Error error) {
- String errorDescription = "";
- if (error is DioError) {
- switch (error.type) {
- case DioErrorType.CANCEL:
- errorDescription = "Request to API server was cancelled";
- break;
- case DioErrorType.CONNECT_TIMEOUT:
- errorDescription = "Connection timeout with API server";
- break;
- case DioErrorType.DEFAULT:
- errorDescription =
- "Connection to API server failed due to internet connection";
- break;
- case DioErrorType.RECEIVE_TIMEOUT:
- errorDescription = "Receive timeout in connection with API server";
- break;
- case DioErrorType.RESPONSE:
- errorDescription =
- "Received invalid status code: ${error.response.statusCode}";
- break;
- }
- } else {
- errorDescription = "Unexpected error occured";
- }
- return errorDescription;
- }
- Future<LocationsResponse> getlocations() async {
- //print('location query data:' + userLocationQuery);
- try {
- Response response = await _dio.get(_apiUrl + '/locations', data: '');
- return LocationsResponse.fromJSON(response.data);
- } catch (error, stacktrace) {
- print("Exception occured: $error stackTrace: $stacktrace");
- return LocationsResponse.withError(_handleError(error));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement