在扑动中,检查权限,实际上请求它们?



我正在学习flutter来构建一个基于地图的应用程序。我正在使用位置pub.dev插件来管理位置权限。我已经创建了一个Location()对象Location location = Location()

当我调用

await location.hasPermission()

我没有授予权限,它实际上请求权限,而不需要我调用

await location.requestPermission()

这会导致许多问题,例如一次请求两个权限,这是Android不允许的,所以应用程序崩溃,或者通过删除await location.requestPermission(),应用程序请求用户的权限,但它不等待结果。

我通过adb在我的Pixel 5上测试了它,运行Android 12

怎么回事?我没有找到关于这个问题的其他参考。

下面是完整的代码示例:

class Gmap extends StatefulWidget {
const Gmap({ Key? key }) : super(key: key);
@override
State<Gmap> createState() => _GmapState();
}
class _GmapState extends State<Gmap> {
String _mapStyle = "";
late GoogleMapController mapController;
late Future<LatLng> ull ;
Location location = Location();
final LatLng _center = const LatLng(37.983810, 23.727539); 
LatLng _userLocation = const LatLng(37.983810, 23.727539);

Future<LatLng> userLocation() async{
PermissionStatus _permissionGranted;
bool _serviceEnabled;
_serviceEnabled = await location.serviceEnabled();
if (!_serviceEnabled) {
_serviceEnabled = await location.requestService();
if (!_serviceEnabled) {
return _center;
}
}
_permissionGranted = await location.hasPermission(); //it requests permission here
if (_permissionGranted == PermissionStatus.denied) {
_permissionGranted = await location.requestPermission()//it requests here again;
if (_permissionGranted != PermissionStatus.granted) {
return _center;
}
}
LocationData l = await location.getLocation();
setState((){
_userLocation  = LatLng(l.latitude!, l.longitude!);
});
return _userLocation;
}
void centerLocation(){
CameraPosition userCamera = CameraPosition(
target: _userLocation,
zoom: 14.0,
);
CameraUpdate moveTo =  CameraUpdate.newCameraPosition(userCamera);    
mapController.animateCamera(moveTo);
}
void _onMapCreated(GoogleMapController controller) async{

mapController = controller;
mapController.setMapStyle(_mapStyle);    
centerLocation();   
}
@override
void initState() {
super.initState();
rootBundle.loadString('assets/style.txt').then((string) { 
_mapStyle = string;
});
ull = userLocation();
}  
Widget googleMap(userLocation){
return GoogleMap(
onMapCreated: _onMapCreated,
myLocationEnabled:true,
initialCameraPosition: CameraPosition(
target: userLocation,
zoom: 6.0,
),
zoomControlsEnabled: false, //dont show zoom buttons
compassEnabled: false, 

myLocationButtonEnabled: false,
);
}

@override
Widget build(BuildContext context){
return Scaffold(
body: FutureBuilder<LatLng>(
future: ull,
builder: ( context , AsyncSnapshot<LatLng> snapshot){
Widget g;
if(snapshot.hasData){            
g = googleMap(_userLocation);            
}else if(snapshot.hasError){
g = googleMap(_center);
}else{
g = googleMap(_center);
}
return g;
}
),

floatingActionButton: Column(mainAxisAlignment: MainAxisAlignment.end, children: <Widget>[
ElevatedButton(
onPressed: () {}, 

style: ElevatedButton.styleFrom(
primary: ourGreen,
shape: const CircleBorder(),
padding: const EdgeInsets.all(15),),
child: const Icon(Icons.filter_alt_rounded, size: 27, color: ourDark),),
const SizedBox(height: 9),
ElevatedButton(
onPressed: () {centerLocation(); }, 
style: ElevatedButton.styleFrom(
primary: const Color(0xFF1A202C),
shape: const CircleBorder(),
padding: const EdgeInsets.all(15)),
child: const Icon(Icons.location_on,
size: 27, color: Colors.greenAccent)),
]),
);
}
}

您可以使用permission_handler包并像这样请求权限状态:

var status = await Permission.camera.status;
if (status.isDenied) {
// We didn't ask for permission yet or the permission has been denied before but not permanently.
}
// You can can also directly ask the permission about its status.
if (await Permission.location.isRestricted) {
// The OS restricts access, for example because of parental controls.
}

参见文档:https://pub.dev/packages/permission_handler

更新:由于文档或其他注释的某些原因,我从来没有得到这个工作。以下是对我有用的:

Future<LatLng> askPermissionAndGetLocation() async {
if (await Permission.locationWhenInUse.serviceStatus.isEnabled) { //this here checks
//if the permission is granted and if not , it requests it. 

if (await Permission.location.request().isGranted) {
LocationData l = await Location().getLocation();
setState(() {
_userLocation = LatLng(l.latitude!, l.longitude!);
});
return _userLocation;
}
}    
return Future.error("Location services disabled or restricted");
}

这是我让它工作的唯一方法,我不明白为什么。如果有人有任何见解,请留下评论

相关内容

  • 没有找到相关文章

最新更新