From a0ece6f9736bdcfa218e2cd97dd2675319c88460 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Sat, 23 Nov 2024 10:50:57 +0100 Subject: [PATCH] passing parameter --- covas_mobile/lib/pages/ListItemMenu.dart | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/covas_mobile/lib/pages/ListItemMenu.dart b/covas_mobile/lib/pages/ListItemMenu.dart index f3dd37d..5020fcb 100644 --- a/covas_mobile/lib/pages/ListItemMenu.dart +++ b/covas_mobile/lib/pages/ListItemMenu.dart @@ -172,9 +172,6 @@ class _MyHomePageState extends State { // Reverse geocode: Get city and country from latitude and longitude using Mapbox Search API if (position != null) { _getCityAndCountry(position!.latitude, position!.longitude); - SharedPreferences prefs = await SharedPreferences.getInstance(); - prefs.setDouble("city_lat", position!.latitude); - prefs.setDouble("city_long", position!.longitude); } } } @@ -206,7 +203,10 @@ class _MyHomePageState extends State { String country = _getCountryFromFeatures(features); print("city : ${city} ${country}"); if (city.isNotEmpty && country.isNotEmpty) { - fetchPostsByLocation(latitude, longitude); + SharedPreferences prefs = await SharedPreferences.getInstance(); + prefs.setDouble("city_lat", latitude); + prefs.setDouble("city_long", longitude); + fetchPostsByLocation(); setState(() { inputGeo.text = "${city}, ${country}"; }); @@ -286,11 +286,14 @@ class _MyHomePageState extends State { } } - Future fetchPostsByLocation(double latitude, double longitude) async { + Future fetchPostsByLocation() async { SharedPreferences prefs = await SharedPreferences.getInstance(); var accessToken = prefs.getString("access_token") ?? ""; if (accessToken.isNotEmpty) { + double latitude = prefs.getDouble("city_lat") ?? 0.0; + double longitude = prefs.getDouble("city_long") ?? 0.0; + // Calculate the boundaries double radiusInKm = 50; double latDistance = radiusInKm / 111.0; @@ -418,12 +421,11 @@ class _MyHomePageState extends State { inputGeo.text = geographicalZone; suggestions.clear(); }); - - await fetchPostsByLocation(latitude, longitude); SharedPreferences prefs = await SharedPreferences.getInstance(); prefs.setDouble("city_lat", latitude); prefs.setDouble("city_long", longitude); + await fetchPostsByLocation(); }, ); },