passing parameter

This commit is contained in:
Valentin CZERYBA 2024-11-23 10:50:57 +01:00
parent c6a0c2f4f1
commit a0ece6f973

View File

@ -172,9 +172,6 @@ class _MyHomePageState extends State<ListItemMenu> {
// Reverse geocode: Get city and country from latitude and longitude using Mapbox Search API // Reverse geocode: Get city and country from latitude and longitude using Mapbox Search API
if (position != null) { if (position != null) {
_getCityAndCountry(position!.latitude, position!.longitude); _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<ListItemMenu> {
String country = _getCountryFromFeatures(features); String country = _getCountryFromFeatures(features);
print("city : ${city} ${country}"); print("city : ${city} ${country}");
if (city.isNotEmpty && country.isNotEmpty) { 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(() { setState(() {
inputGeo.text = "${city}, ${country}"; inputGeo.text = "${city}, ${country}";
}); });
@ -286,11 +286,14 @@ class _MyHomePageState extends State<ListItemMenu> {
} }
} }
Future<void> fetchPostsByLocation(double latitude, double longitude) async { Future<void> fetchPostsByLocation() async {
SharedPreferences prefs = await SharedPreferences.getInstance(); SharedPreferences prefs = await SharedPreferences.getInstance();
var accessToken = prefs.getString("access_token") ?? ""; var accessToken = prefs.getString("access_token") ?? "";
if (accessToken.isNotEmpty) { if (accessToken.isNotEmpty) {
double latitude = prefs.getDouble("city_lat") ?? 0.0;
double longitude = prefs.getDouble("city_long") ?? 0.0;
// Calculate the boundaries // Calculate the boundaries
double radiusInKm = 50; double radiusInKm = 50;
double latDistance = radiusInKm / 111.0; double latDistance = radiusInKm / 111.0;
@ -418,12 +421,11 @@ class _MyHomePageState extends State<ListItemMenu> {
inputGeo.text = geographicalZone; inputGeo.text = geographicalZone;
suggestions.clear(); suggestions.clear();
}); });
await fetchPostsByLocation(latitude, longitude);
SharedPreferences prefs = SharedPreferences prefs =
await SharedPreferences.getInstance(); await SharedPreferences.getInstance();
prefs.setDouble("city_lat", latitude); prefs.setDouble("city_lat", latitude);
prefs.setDouble("city_long", longitude); prefs.setDouble("city_long", longitude);
await fetchPostsByLocation();
}, },
); );
}, },