首页 > 解决方案 > 如果 GPS 在 Flutter 的后台被禁用,应用程序崩溃

问题描述

我正在使用下面的代码来检查 GPS 是否启用,如果没有,请询​​问许可,一旦启用,用户可以导航到下一个屏幕(下一个屏幕只有一个标签。下一个屏幕中没有其他功能)。它工作正常。

问题是:如果我执行以下操作,它会使我的应用程序崩溃 - 1)允许位置权限,即启用 GPS 2)用户导航到下一个屏幕 3)应用程序移动背景。然后应用程序移动到前台一切正常。但是,如果应用程序移动背景并且我从设置中禁用了 GPS - 它会使应用程序在后台本身崩溃。

import 'package:geolocator/geolocator.dart';


 bool isLocationEnabled = await Geolocator().isLocationServiceEnabled();
        GeolocationStatus _permission;

        if (isLocationEnabled) {
          print("enable");
          _permission = await Geolocator().checkGeolocationPermissionStatus();
          print("Permission result: $_permission");
          if (_permission == GeolocationStatus.granted) {
            Navigator.of(context).pushNamed(HOME_SCREEN);
          }else{
            print("permission not enable");
          }
        } else {
          print("not enable");
        }

标签: androidflutterlocation

解决方案


这背后的真正原因是“java.lang.IllegalStateException:回复已提交”,您可以在 github 上查看此线程:

https://github.com/flutter/flutter/issues/29092

您可以在 java 类中导致问题的行周围添加 try catch 并打印一些消息,而不是让应用程序崩溃,直到找到解决方案。


推荐阅读