Flutter 当用户拒绝 GPS 权限或未打开 google 地图 GPS 时如何处理

发布于 2025-01-13 14:44:49 字数 2139 浏览 4 评论 0原文

我正在使用 Google 地图包 在我的应用程序上显示 google 地图。默认情况下,它会请求允许应用程序使用 GPS,当授予权限但用户关闭了 GPS 时,它会要求他们将其打开。
目前,当用户拒绝 GPS 权限或用户选择“不,谢谢”并且不打开设备上的 GPS 时,我不知道如何处理这两个位置。因此,它会因未处理的异常而导致应用程序崩溃。
发生异常。 _CastError(空值检查运算符)

我想让它通过显示一个小部件来处理这两种情况,该小部件可以说明任何这些情况,而不是使应用程序崩溃。

位置包(请求许可的位置)

import 'package:geolocator/geolocator.dart';

class Location {
double? latitude;
double? longitude;

static LocationPermission? permission;

Future<void> getCurrentLocation() async {
permission = await Geolocator.requestPermission();
try {
  Position position = await Geolocator.getCurrentPosition(
      desiredAccuracy: LocationAccuracy.low);
  latitude = position.latitude;
  longitude = position.longitude;
} catch (e) {
  print(e);
   }
 }
}

加载屏幕(从 GeoLocator 包检索当前位置数据的位置)

class _NearbyMechanicLoadingState extends 
State<NearbyMechanicLoading> {
bool isPopAllowed = false;
Location myLocation = Location();

void loadingData() async {
   await myLocation.getCurrentLocation();
   Navigator.pushReplacement(
    context,
    MaterialPageRoute(
      builder: (context) => NavigatingPage(
        title: 'Nearby Mechanics',
        page: NearbyMechanicPage(
          lat: myLocation.latitude,
          long: myLocation.longitude,
        ),
      ),
    ));
  }

 @override
 void initState() {
 super.initState();
 loadingData();
}

Google 地图屏幕

class _NearbyMechanicPageState extends State<NearbyMechanicPage> {
GoogleMapController? _googleMapController;

@override
void dispose() {
   _googleMapController!.dispose();
   super.dispose();
 } 

@override
Widget build(BuildContext context) {
    return GoogleMap(
       mapType: MapType.terrain,
       myLocationButtonEnabled: false,
       zoomControlsEnabled: false,
       initialCameraPosition:
       CameraPosition(target: LatLng(widget.lat!, widget.long!), 
       zoom:15.0),
       onMapCreated: (controller) => _googleMapController = controller,
      );
    }
   }

I'm using Google Maps package for displaying google map on my application. by default it asks for a permission to let the application use the GPS and when the permission is granted but the user has the GPS turned off, it asks them to turn it on.
Currently, i don't know how to handle these two sitations when the user denies the GPS permission or when the user choose 'No thanks' and doesn't turn on the GPS on their device. Therfore, it crashes the app for an unhandled exception.
Exception has occurred. _CastError (Null check operator used on a null value)

I want to make it handle these two situations by showing a widget that stating any of these situations rather than crashing the application.

Location Package (Where it asks for the Permission)

import 'package:geolocator/geolocator.dart';

class Location {
double? latitude;
double? longitude;

static LocationPermission? permission;

Future<void> getCurrentLocation() async {
permission = await Geolocator.requestPermission();
try {
  Position position = await Geolocator.getCurrentPosition(
      desiredAccuracy: LocationAccuracy.low);
  latitude = position.latitude;
  longitude = position.longitude;
} catch (e) {
  print(e);
   }
 }
}

Loading Screen (Where it retrieve the current location data from GeoLocator package)

class _NearbyMechanicLoadingState extends 
State<NearbyMechanicLoading> {
bool isPopAllowed = false;
Location myLocation = Location();

void loadingData() async {
   await myLocation.getCurrentLocation();
   Navigator.pushReplacement(
    context,
    MaterialPageRoute(
      builder: (context) => NavigatingPage(
        title: 'Nearby Mechanics',
        page: NearbyMechanicPage(
          lat: myLocation.latitude,
          long: myLocation.longitude,
        ),
      ),
    ));
  }

 @override
 void initState() {
 super.initState();
 loadingData();
}

Google map screen

class _NearbyMechanicPageState extends State<NearbyMechanicPage> {
GoogleMapController? _googleMapController;

@override
void dispose() {
   _googleMapController!.dispose();
   super.dispose();
 } 

@override
Widget build(BuildContext context) {
    return GoogleMap(
       mapType: MapType.terrain,
       myLocationButtonEnabled: false,
       zoomControlsEnabled: false,
       initialCameraPosition:
       CameraPosition(target: LatLng(widget.lat!, widget.long!), 
       zoom:15.0),
       onMapCreated: (controller) => _googleMapController = controller,
      );
    }
   }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

行雁书 2025-01-20 14:44:49

您可以使用 permission_handler 包来请求权限并查看他们是否授予权限。

如果用户(永久)拒绝权限,您可以显示一个对话框/屏幕,提及他们需要更改设置中的权限。 openAppSettings

如果用户没有打开他们的位置,您可以显示一个对话框/屏幕,提及他们需要打开他们的位置。您可以使用 Geolocator.openLocationSettings(); 来实现此目的。

You can use the permission_handler package to ask for permission and see if they gave permission.

If the user (permanently) denied permission, you can display a dialog/screen mentioning that they need to change the permission in settings. openAppSettings.

If the user didn't turn on their location, you can display a dialog/screen mentioning that they need to turn on their location. You can use Geolocator.openLocationSettings(); for this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文