android - 如果 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");
}
解决方案
这背后的真正原因是“java.lang.IllegalStateException:回复已提交”,您可以在 github 上查看此线程:
https://github.com/flutter/flutter/issues/29092
您可以在 java 类中导致问题的行周围添加 try catch 并打印一些消息,而不是让应用程序崩溃,直到找到解决方案。
推荐阅读
- sql - 操作数数据类型数字对于“~”运算符无效
- python - 需要将元组转换为pyaudio的字节
- sql - 在 SQL 中有没有办法在连接上使用 select *?
- firebase - 错误:3 INVALID_ARGUMENT:无法将数组值转换为数组值
- javascript - 在 Google Maps 元素上添加背景叠加效果
- c# - 类似于 Unity 的 Transform 组件的 C# 库
- tensorflow2.0 - 如何将我的数据集转换为 Tensorflow.keras.model.fit()
- sql - [Postgres/SQL 查询]按 date_trunc 分组时如何获取最后一行条目?
- c# - CSR 使用 SHA-256withECDSA 的签名无效
- php - 删除空白但保留段落