coordernadas

This commit is contained in:
Juan Felipe Duarte
2023-06-16 15:42:07 -05:00
parent b7b246b03a
commit f69201e0dd
4 changed files with 93 additions and 30 deletions
+32 -14
View File
@@ -17,13 +17,20 @@ final CollectionReference countriesCollection =
class City {
String? cityName;
String? coordsOfCity;
String? stateOfCity;
String? countryOfCity;
City({this.cityName, this.stateOfCity, this.countryOfCity});
City({
this.cityName,
this.coordsOfCity,
this.stateOfCity,
this.countryOfCity,
});
@override
String toString() {
return "${cityName ?? ""}, ${stateOfCity ?? ""}, ${countryOfCity ?? ""}";
return "${cityName ?? ""}, ${coordsOfCity ?? ""}, ${stateOfCity ?? ""}, ${countryOfCity ?? ""}";
}
}
@@ -35,27 +42,27 @@ Future<List<City>> getCountries() async {
for (DocumentSnapshot country in countries.docs) {
String countryName = country.id;
Map<String, dynamic> data = country.data() as Map<String, dynamic>;
Map<String, List<String>> states = {};
Map<String, Map<String, String>> states = {};
for (var entry in data.entries) {
String key = entry.key;
List<String> value = List<String>.from(entry.value);
states[key] = value;
Map<String, String> cityData = Map<String, String>.from(entry.value);
states[key] = cityData;
}
for (var state in states.entries) {
var citysState = state.value
.map((cityName) => City(
cityName: cityName,
stateOfCity: state.key,
countryOfCity: countryName))
.toList();
var citysState = state.value.entries.map((city) => City(
cityName: city.key,
coordsOfCity: city.value,
stateOfCity: state.key,
countryOfCity: countryName,
));
citys.addAll(citysState);
}
}
} catch (e) {
print('xd $e');
print('xd seqe $e');
}
return citys;
@@ -98,13 +105,18 @@ class _CityScreenState extends State<CityScreen> {
}
}
Future<void> updateCity(String cityName) async {
Future<void> updateCity(String cityName, String coordsCity) async {
try {
await FirebaseFirestore.instance
.collection('users')
.doc(uid)
.update({'city': cityName});
await FirebaseFirestore.instance
.collection('users')
.doc(uid)
.update({'coordsOfCity': coordsCity});
print('Ciudad actualizada correctamente');
} catch (e) {
try {
@@ -112,6 +124,11 @@ class _CityScreenState extends State<CityScreen> {
.collection('users')
.doc(uid)
.set({'city': cityName});
await FirebaseFirestore.instance
.collection('users')
.doc(uid)
.set({'coordsOfCity': coordsCity});
} catch (e) {
print('Error al agregar la ciudad: $e');
}
@@ -175,7 +192,8 @@ class _CityScreenState extends State<CityScreen> {
),
),
onTap: () {
updateCity(citys[index].cityName ?? "");
updateCity(citys[index].cityName ?? "",
citys[index].coordsOfCity ?? "");
Navigator.pop(context, citys[index].cityName ?? "");
},
);