I am constructing a Flutter app that requires location information. I am utilizing the ‘Geolocator’ and ‘Location’ plugin to retrieve the person’s coordinates. Once I set the simulator location to “Customized Location”, the situation.getLocation() technique would not appear to execute. Nevertheless, if I set it to a predefined location like “Apple”, it really works wonderful.
Right here is my full Code:
import 'package deal:flutter/materials.dart';
import 'package deal:location/location.dart' as loc;
import 'package deal:geocoding/geocoding.dart';
void principal() {
runApp(MaterialApp(dwelling: MyApp()));
}
class MyApp extends StatefulWidget {
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
void initState() {
location();
tremendous.initState();
}
String? _city;
String? _country;
Future<void> location() async {
loc.Location location = loc.Location();
bool serviceEnabled;
loc.PermissionStatus permissionGranted;
loc.LocationData? locationData;
serviceEnabled = await location.serviceEnabled();
if (!serviceEnabled) {
serviceEnabled = await location.requestService();
if (!serviceEnabled) {
return;
}
}
permissionGranted = await location.hasPermission();
if (permissionGranted == loc.PermissionStatus.denied) {
permissionGranted = await location.requestPermission();
if (permissionGranted != loc.PermissionStatus.granted) {
return;
}
}
print('will get all the time printed');
locationData = await location.getLocation();
print('This isn't getting printed when location is ready on "Customized Location"');
print(locationData);
Listing<Placemark> placemarks = await placemarkFromCoordinates(
locationData.latitude!, locationData.longitude!);
setState(() {
_city = placemarks[0].locality!;
_country = placemarks[0].nation!;
});
}
@override
Widget construct(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colours.pink,
title:const Textual content('Location'),
),
physique: Heart(
youngster: Column(
mainAxisAlignment: MainAxisAlignment.middle,
youngsters: <Widget>[
const Text(
'Coordinates:', style: TextStyle(fontSize: 20)
),
Column(
children: [
Text("$_city, $_country", style:const TextStyle(fontSize: 30),)
],
),
],
),
),
);
}
}