Been trying to use geolocator to get device location (lon and lat to be exact) and send them to another widget for api request but I can't use Geolocator.getCurrentPosition().longitude
nor Geolocator.getCurrentPosition().latitude
and here is my latest code.
import 'package:flutter/material.dart';
import 'package:geolocator/geolocator.dart';
/// Determine the current position of the device.
/// When the location services are not enabled or permissions
/// are denied the `Future` will return an error.
Future<Position> fetchPos(String input) async {
//String input;
bool serviceEnabled;
LocationPermission permission;
serviceEnabled = await Geolocator.isLocationServiceEnabled();
if (!serviceEnabled) {
return Future.error('Location services are disabled.');
}
permission = await Geolocator.checkPermission();
if (permission == LocationPermission.deniedForever) {
return Future.error(
'Location permissions are permantly denied, we cannot request permissions.');
}
if (permission == LocationPermission.denied) {
permission = await Geolocator.requestPermission();
if (permission != LocationPermission.whileInUse &&
permission != LocationPermission.always) {
return Future.error(
'Location permissions are denied (actual value: $permission).');
}
}
if (input == "lat") {
return await Geolocator.getCurrentPosition();
} else if (input == "lon") {
return await Geolocator.getCurrentPosition();
}
}
class GeoLoc extends StatefulWidget {
String input;
GeoLoc(this.input);
@override
_GeoLocState createState() => _GeoLocState(input);
}
class _GeoLocState extends State<GeoLoc> {
//double _lat = 0;
//double _lon = 0;
String _output;
Future<Position> futurePos;
_GeoLocState(String input);
@override
void initState() {
super.initState();
futurePos = fetchPos("lat");
if (widget.input == "lat") {
_output = "${fetchPos("lat")}";
} else if (widget.input == "lon") {
_output = "${fetchPos("lon")}";
}
}
@override
Widget build(BuildContext context) {
return Container(
child: FutureBuilder<Position>(
future: futurePos,
builder: (context, snapshot) {
if (snapshot.hasData) {
return Text(
snapshot.data.longitude.toString(),
);
} else if (snapshot.hasError){
return Text("${snapshot.error}");
}return CircularProgressIndicator();
}),
);
}
}
this is where I want my lon and lat to be
Future<Album> fetchAlbum() async {
//TODO change 524901 with GPS 'api.openweathermap.org/data/2.5/weather?lat={lat}&lon={lon}&appid={id}');
final response = await http.get(
'https://api.openweathermap.org/data/2.5/weather?id=524901&appid={id}');
if (response.statusCode == 200) {
// If the server did return a 200 OK response,
// then parse the JSON.
return Album.fromJson(jsonDecode(response.body));
} else {
// If the server did not return a 200 OK response,
// then throw an exception.
throw Exception('Failed to load album');
}
}
both are sperate files.
ps: there is some random nonsense just for testing purposes.
question from:
https://stackoverflow.com/questions/65939829/flutter-using-geolocator-to-obtain-lon-and-lat 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…