143 lines
4.2 KiB
Dart
143 lines
4.2 KiB
Dart
import 'dart:convert';
|
|
|
|
import 'package:unit2/model/location/address_category.dart';
|
|
import 'package:unit2/model/location/barangay.dart';
|
|
import 'package:unit2/model/location/city.dart';
|
|
import 'package:unit2/model/location/country.dart';
|
|
import 'package:http/http.dart' as http;
|
|
import 'package:unit2/model/location/provinces.dart';
|
|
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;
|
|
|
|
Map<String, String> headers = {
|
|
'Content-Type': 'application/json; charset=UTF-8',
|
|
};
|
|
|
|
Future<List<Country>> getCountries() async {
|
|
List<Country> countries = [];
|
|
String path = Url.instance.getCounties();
|
|
|
|
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();
|
|
|
|
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;
|
|
}
|
|
|
|
Future<List<Province>> getProvinces({required String regionCode}) async {
|
|
List<Province> provinces = [];
|
|
String path = Url.instance.getProvinces() + regionCode;
|
|
|
|
try {
|
|
http.Response response = await Request.instance
|
|
.getRequest(path: path, param: {}, headers: headers);
|
|
Map data = jsonDecode(response.body);
|
|
if (data['data'] != null) {
|
|
data['data'].forEach((var province) {
|
|
Province newProvince = Province.fromJson(province);
|
|
provinces.add(newProvince);
|
|
});
|
|
}
|
|
} catch (e) {
|
|
throw (e.toString());
|
|
}
|
|
return provinces;
|
|
}
|
|
|
|
Future<List<CityMunicipality>> getCities({required String code}) async {
|
|
List<CityMunicipality> cities = [];
|
|
String path = Url.instance.getCities() + code;
|
|
try {
|
|
http.Response response = await Request.instance
|
|
.getRequest(path: path, param: {}, headers: headers);
|
|
Map data = jsonDecode(response.body);
|
|
if (data['data'] != null) {
|
|
data['data'].forEach((var city) {
|
|
CityMunicipality cityMun = CityMunicipality.fromJson(city);
|
|
cities.add(cityMun);
|
|
});
|
|
}
|
|
} catch (e) {
|
|
throw (e.toString());
|
|
}
|
|
return cities;
|
|
}
|
|
|
|
Future<List<Barangay>> getBarangay({required String code}) async {
|
|
List<Barangay> barangays = [];
|
|
String path = Url.instance.getBarangays() + code;
|
|
try {
|
|
http.Response response = await Request.instance
|
|
.getRequest(path: path, param: {}, headers: headers);
|
|
Map data = jsonDecode(response.body);
|
|
if (data['data'] != null) {
|
|
data['data'].forEach((var city) {
|
|
Barangay barangay = Barangay.fromJson(city);
|
|
barangays.add(barangay);
|
|
});
|
|
}
|
|
} catch (e) {
|
|
throw (e.toString());
|
|
}
|
|
return barangays;
|
|
}
|
|
Future<List<AddressCategory>>getAddressCategory()async{
|
|
List<AddressCategory> categories = [];
|
|
String path = Url.instance.getAddressCategory();
|
|
try{
|
|
http.Response response = await Request.instance.getRequest(path: path,param: {},headers:headers );
|
|
Map data = jsonDecode(response.body);
|
|
if(data['data'] != null){
|
|
data['data'].forEach((var cat){
|
|
categories.add(AddressCategory.fromJson(cat));
|
|
});
|
|
}
|
|
categories;
|
|
return categories;
|
|
}catch(e){
|
|
throw e.toString();
|
|
}
|
|
}
|
|
|
|
|
|
}
|