Я строю приложение Flutter, которое требует данных о местоположении. Я использую плагин «геолокатор» и «местоположение», чтобы получить координаты пользователя. Когда я устанавливаю местоположение симулятора в «пользовательское местоположение», метод местоположения. GetLocation (), похоже, не выполняется. Однако, если я устанавливаю его в предопределенное место, как «Apple», это работает нормально. < /P>
Вот мой полный код: < /p>
import 'package:flutter/material.dart';
import 'package:location/location.dart' as loc;
import 'package:geocoding/geocoding.dart';
void main() {
runApp(MaterialApp(home: MyApp()));
}
class MyApp extends StatefulWidget {
@override
State createState() => _MyAppState();
}
class _MyAppState extends State {
@override
void initState() {
location();
super.initState();
}
String? _city;
String? _country;
Future 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('gets always printed');
locationData = await location.getLocation();
print('This is not getting printed when location is set on "Custom Location"');
print(locationData);
List placemarks = await placemarkFromCoordinates(
locationData.latitude!, locationData.longitude!);
setState(() {
_city = placemarks[0].locality!;
_country = placemarks[0].country!;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.red,
title:const Text('Location'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Text(
'Coordinates:', style: TextStyle(fontSize: 20)
),
Column(
children: [
Text("$_city, $_country", style:const TextStyle(fontSize: 30),)
],
),
],
),
),
);
}
}
Подробнее здесь: https://stackoverflow.com/questions/771 ... -with-cust