2023-02-15 08:48:34 +00:00
|
|
|
import 'dart:convert';
|
|
|
|
|
2023-03-24 06:46:17 +00:00
|
|
|
import 'package:unit2/model/location/address_category.dart';
|
|
|
|
import 'package:unit2/model/location/barangay.dart';
|
2023-02-20 07:48:24 +00:00
|
|
|
import 'package:unit2/model/location/city.dart';
|
2023-02-15 08:48:34 +00:00
|
|
|
import 'package:unit2/model/location/country.dart';
|
|
|
|
import 'package:http/http.dart' as http;
|
2023-02-16 07:10:54 +00:00
|
|
|
import 'package:unit2/model/location/provinces.dart';
|
2023-02-15 08:48:34 +00:00
|
|
|
import 'package:unit2/utils/request.dart';
|
|
|
|
import 'package:unit2/utils/urls.dart';
|
|
|
|
|
|
|
|
import '../model/location/region.dart';
|
2023-03-24 06:46:17 +00:00
|
|
|
|
2023-02-15 08:48:34 +00:00
|
|
|
class LocationUtils {
|
|
|
|
static final LocationUtils _instance = LocationUtils();
|
|
|
|
static LocationUtils get instance => _instance;
|
|
|
|
|
2023-03-24 06:46:17 +00:00
|
|
|
Map<String, String> headers = {
|
|
|
|
'Content-Type': 'application/json; charset=UTF-8',
|
|
|
|
};
|
2023-02-16 07:10:54 +00:00
|
|
|
|
2023-03-24 06:46:17 +00:00
|
|
|
Future<List<Country>> getCountries() async {
|
|
|
|
List<Country> countries = [];
|
2023-02-15 08:48:34 +00:00
|
|
|
String path = Url.instance.getCounties();
|
2023-02-16 07:10:54 +00:00
|
|
|
|
2023-03-24 06:46:17 +00:00
|
|
|
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);
|
|
|
|
});
|
|
|
|
}
|
2023-02-15 08:48:34 +00:00
|
|
|
}
|
2023-03-24 06:46:17 +00:00
|
|
|
}catch(e){
|
|
|
|
throw(e.toString());
|
|
|
|
}
|
|
|
|
return countries;
|
2023-02-15 08:48:34 +00:00
|
|
|
}
|
|
|
|
|
2023-03-24 06:46:17 +00:00
|
|
|
Future<List<Region>> getRegions() async {
|
|
|
|
List<Region> regions = [];
|
2023-02-15 08:48:34 +00:00
|
|
|
String path = Url.instance.getRegions();
|
2023-03-24 06:46:17 +00:00
|
|
|
|
|
|
|
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());
|
2023-02-15 08:48:34 +00:00
|
|
|
}
|
2023-03-24 06:46:17 +00:00
|
|
|
return regions;
|
2023-02-15 08:48:34 +00:00
|
|
|
}
|
2023-02-16 07:10:54 +00:00
|
|
|
|
2023-03-24 06:46:17 +00:00
|
|
|
Future<List<Province>> getProvinces({required String regionCode}) async {
|
|
|
|
List<Province> provinces = [];
|
|
|
|
String path = Url.instance.getProvinces() + regionCode;
|
2023-02-16 07:10:54 +00:00
|
|
|
|
2023-03-24 06:46:17 +00:00
|
|
|
try {
|
|
|
|
http.Response response = await Request.instance
|
|
|
|
.getRequest(path: path, param: {}, headers: headers);
|
2023-02-16 07:10:54 +00:00
|
|
|
Map data = jsonDecode(response.body);
|
2023-03-24 06:46:17 +00:00
|
|
|
if (data['data'] != null) {
|
|
|
|
data['data'].forEach((var province) {
|
2023-02-16 07:10:54 +00:00
|
|
|
Province newProvince = Province.fromJson(province);
|
|
|
|
provinces.add(newProvince);
|
|
|
|
});
|
|
|
|
}
|
2023-03-24 06:46:17 +00:00
|
|
|
} catch (e) {
|
|
|
|
throw (e.toString());
|
2023-02-16 07:10:54 +00:00
|
|
|
}
|
2023-03-24 06:46:17 +00:00
|
|
|
return provinces;
|
2023-02-16 07:10:54 +00:00
|
|
|
}
|
2023-02-20 07:48:24 +00:00
|
|
|
|
2023-03-24 06:46:17 +00:00
|
|
|
Future<List<CityMunicipality>> getCities({required String code}) async {
|
2023-02-20 07:48:24 +00:00
|
|
|
List<CityMunicipality> cities = [];
|
2023-03-24 06:46:17 +00:00
|
|
|
String path = Url.instance.getCities() + code;
|
|
|
|
try {
|
|
|
|
http.Response response = await Request.instance
|
|
|
|
.getRequest(path: path, param: {}, headers: headers);
|
2023-02-20 07:48:24 +00:00
|
|
|
Map data = jsonDecode(response.body);
|
2023-03-24 06:46:17 +00:00
|
|
|
if (data['data'] != null) {
|
|
|
|
data['data'].forEach((var city) {
|
2023-02-20 07:48:24 +00:00
|
|
|
CityMunicipality cityMun = CityMunicipality.fromJson(city);
|
|
|
|
cities.add(cityMun);
|
|
|
|
});
|
|
|
|
}
|
2023-03-24 06:46:17 +00:00
|
|
|
} catch (e) {
|
|
|
|
throw (e.toString());
|
2023-02-20 07:48:24 +00:00
|
|
|
}
|
|
|
|
return cities;
|
|
|
|
}
|
2023-03-24 06:46:17 +00:00
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|