passo_mobile_app/lib/utils/location_utilities.dart

61 lines
1.7 KiB
Dart

import 'dart:convert';
import 'package:unit2/model/location/country.dart';
import 'package:http/http.dart' as http;
import 'package:unit2/utils/request.dart';
import 'package:unit2/utils/urls.dart';
import '../model/location/region.dart';
class LocationUtils {
static final LocationUtils _instance = LocationUtils();
static LocationUtils get instance => _instance;
Future<List<Country>>getCountries()async{
List<Country> countries=[];
String path = Url.instance.getCounties();
Map<String, String> headers = {
'Content-Type': 'application/json; charset=UTF-8',
};
// try{
http.Response response = await Request.instance.getRequest(path: path, param: {},headers: headers);
if(response.statusCode == 200){
Map data = jsonDecode(response.body);
if(data['data'] != null){
data['data'].forEach((var country){
Country newCOuntry = Country.fromJson(country);
countries.add(newCOuntry);
});
}
}
// }catch(e){
// throw(e.toString());
// }
return countries;
}
Future<List<Region>>getRegions()async{
List<Region> regions=[];
String path = Url.instance.getRegions();
Map<String, String> headers = {
'Content-Type': 'application/json; charset=UTF-8',
};
// try{
http.Response response = await Request.instance.getRequest(path: path, param: {},headers: headers);
if(response.statusCode == 200){
Map data = jsonDecode(response.body);
if(data['data'] != null){
data['data'].forEach((var region){
Region newRegion = Region.fromJson(region);
regions.add(newRegion);
});
}
}
// }catch(e){
// throw(e.toString());
// }
return regions;
}
}