Flutter 颤振:启用GPS后运行功能

Flutter 颤振:启用GPS后运行功能,flutter,Flutter,一旦用户启用GPS/定位服务,我想运行下面写的deviceInfo函数。当前,它正在运行,只是为了检查位置服务是否已启用。如果禁用,则显示警报对话框 正如你所看到的,我正在使用If条件。如果位置被禁用,则显示警报对话框,否则继续下一步 问题是,若用户启用位置服务,那个么我如何执行在else条件下编写的代码 这是代码 Future<void> deviceInfo() async { DeviceInfoPlugin deviceInfo = DeviceInfoPlu

一旦用户启用GPS/定位服务,我想运行下面写的deviceInfo函数。当前,它正在运行,只是为了检查位置服务是否已启用。如果禁用,则显示警报对话框

正如你所看到的,我正在使用If条件。如果位置被禁用,则显示警报对话框,否则继续下一步

问题是,若用户启用位置服务,那个么我如何执行在else条件下编写的代码

这是代码

  Future<void> deviceInfo() async {
      DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
      AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
      print(androidInfo.androidId);
      print('Is Physical Device: ${androidInfo.isPhysicalDevice}');
      physicaldevice = androidInfo.isPhysicalDevice;
      if(physicaldevice == true){
       
        var isGpsEnabled = await Geolocator().isLocationServiceEnabled();

         if(isGpsEnabled == false){
          setState(() {
              visible = false;
          });
          _showAlert(context);
          print(isGpsEnabled);
         

        }else{
         ------ Some Codes if GPS is enabled ------
    }
Future deviceInfo()异步{
deviceInfo插件deviceInfo=deviceInfo插件();
AndroidDeviceInfo androidInfo=等待deviceInfo.androidInfo;
印刷品(androidInfo.androidId);
打印('是物理设备:${androidInfo.isPhysicalDevice}');
物理设备=androidInfo.isPhysicalDevice;
如果(physicaldevice==true){
var isGpsEnabled=等待地理定位器().isLocationServiceEnabled();
if(isGpsEnabled==false){
设置状态(){
可见=假;
});
_showAlert(上下文);
打印(isGpsEnabled);
}否则{
------如果启用GPS,则显示一些代码------
}
这是GPS启用消息的警报框

   void _showAlert(BuildContext context) {
      showDialog(
          context: context,
          builder: (context) => AlertDialog(
            title: Text("Error"),
            content: Text("Location Service is disabled. Please enable it."),
             actions: <Widget>[
              FlatButton(child: Text('Ok'),
              onPressed: () {
                final AndroidIntent intent = AndroidIntent(
                  action: 'android.settings.LOCATION_SOURCE_SETTINGS');
              intent.launch();
              Navigator.of(context, rootNavigator: true).pop();
              })],
          )
      );
    }
void\u showAlert(构建上下文){
显示对话框(
上下文:上下文,
生成器:(上下文)=>AlertDialog(
标题:文本(“错误”),
内容:文本(“位置服务已禁用,请启用。”),
行动:[
FlatButton(子项:文本('Ok'),
已按下:(){
最终雄激素意图=雄激素(
操作:“android.settings.LOCATION_SOURCE_settings”);
intent.launch();
of(context,rootNavigator:true).pop();
})],
)
);
}

简单地说,一旦启用GPS,我想重新运行DeviceInfo功能。

您可以尝试使用Timer.periodic如果isGpsEnabled为false

    Future<void> deviceInfo() async {
  DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
  AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
  print(androidInfo.androidId);
  print('Is Physical Device: ${androidInfo.isPhysicalDevice}');
  physicaldevice = androidInfo.isPhysicalDevice;
  if(physicaldevice == true){
   
    var isGpsEnabled = await Geolocator().isLocationServiceEnabled();

     if(isGpsEnabled == false){
      setState(() {
          visible = false;
      });
      _showAlert(context);
      print(isGpsEnabled);
       // new code //
       Timer.periodic(Duration(seconds: 1), (timer) async {
       var isGpsEnabled = await Geolocator().isLocationServiceEnabled();
       if (isGpsEnabled) {
       timer.cancel();
        ------ Some Codes if GPS is enabled ------
       }
});
  
     

    }else{
     ------ Some Codes if GPS is enabled ------
}
Future deviceInfo()异步{
deviceInfo插件deviceInfo=deviceInfo插件();
AndroidDeviceInfo androidInfo=等待deviceInfo.androidInfo;
印刷品(androidInfo.androidId);
打印('是物理设备:${androidInfo.isPhysicalDevice}');
物理设备=androidInfo.isPhysicalDevice;
如果(physicaldevice==true){
var isGpsEnabled=等待地理定位器().isLocationServiceEnabled();
if(isGpsEnabled==false){
设置状态(){
可见=假;
});
_showAlert(上下文);
打印(isGpsEnabled);
//新代码//
定时器。周期(持续时间(秒:1),(定时器)异步{
var isGpsEnabled=等待地理定位器().isLocationServiceEnabled();
如果(isGpsEnabled){
timer.cancel();
------如果启用GPS,则显示一些代码------
}
});
}否则{
------如果启用GPS,则显示一些代码------
}

您的解决方案成功了。我只面临一个问题。如果用户未启用位置并返回,则警报框将不再可见,因为用户已单击“确定”。我设法使其工作。Navigator.of(context,rootNavigator:true)。pop(_showart);在Time.Periodic内添加了此代码。非常感谢您的帮助。