I have a statfull class called 'Location' which get the location with Geolocatar and Im showing the location with this :
Text(currentPosition!=null? '$currentPosition' : '----',),
and this is :
static Position? currentPosition;
and I have another class called 'WeatherApi' which Im using Openweathermap Api to get the weather . now I want to use the currentPosition in the api url
https://api.openweathermap.org/data/2.5/weather?lat=44.34&lon=10.99&appid={API key}
how can I use the currentPosition in the url? I have tried widget.variablename but didn't work
CodePudding user response:
You can pass the location details through the constructor of Location class.
CodePudding user response:
You can define a global variable outside the 'Location' widget as a 'currentPosition',
class Position {
static String? currentPosition;
}
call from 'Location' this variable and set it.
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
void initState() {
Position.currentPosition = 'currentPosition';
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: TextButton(
onPressed: () {
Navigator.of(context).push(
MaterialPageRoute(
builder: ((context) => LocationWidget()),
),
);
},
child: Text('Click Me'),
),
),
);
}
}
Then call it again from 'WeatherApi' and use it.
class WeatherWidget extends StatefulWidget {
const WeatherWidget({super.key});
@override
State<WeatherWidget> createState() => _WeatherWidgetState();
}
class _WeatherWidgetState extends State<WeatherWidget> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Text(Position.currentPosition ?? 'null'),
),
);
}
}