Flutter 禁用GPS时无法打印或获取默认位置

Flutter 禁用GPS时无法打印或获取默认位置,flutter,dart,gps,location,Flutter,Dart,Gps,Location,我有以下基本功能来使用GPS获取用户的当前位置。如果启用GPS,console print(控制台打印)始终打印用户的当前位置,但如果未启用GPS,则不会打印默认静态值。有人可以使用定位和地理定位软件包帮助正确评估检查GPS。下面是完整的代码:默认值应在用户单击“请求权限”对话框上的“否谢谢”后立即打印,而不是在页面刷新后打印,而实际的当前位置应在用户单击“请求”对话框上的“确定”时打印-即,根据用户启用或删除设置或获取位置未启用gps或权限 import 'dart:async';

我有以下基本功能来使用GPS获取用户的当前位置。如果启用GPS,console print(控制台打印)始终打印用户的当前位置,但如果未启用GPS,则不会打印默认静态值。有人可以使用定位和地理定位软件包帮助正确评估检查GPS。下面是完整的代码:默认值应在用户单击“请求权限”对话框上的“否谢谢”后立即打印,而不是在页面刷新后打印,而实际的当前位置应在用户单击“请求”对话框上的“确定”时打印-即,根据用户启用或删除设置或获取位置未启用gps或权限

      import 'dart:async';
  import 'package:flutter/cupertino.dart';
  import 'package:flutter/material.dart';
  import 'package:geolocator/geolocator.dart';
  import 'package:location/location.dart' as loc;

  class GetLocation extends StatefulWidget {
    @override
    State<StatefulWidget> createState() => _GetLocationState();
  }

  class _GetLocationState extends State<GetLocation> {
    final Geolocator geolocator = Geolocator()..forceAndroidLocationManager;
    loc.Location location = loc.Location();

    double usersCurrentLocationLat = 1.2921;
    double usersCurrentLocationLong = 30.8219;

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

    @override
    Widget build(BuildContext context) {
      return Container(
        color: Colors.white,
        child: Scaffold(
          backgroundColor: Colors.white,
          body: Container(
            child: Center(
              child: Text("TTTTTTT"),
              // child: Text(usersCurrentLocationLat.toString() +
              //     " , " +
              //     usersCurrentLocationLong.toString()),
            ),
          ),
        ),
      );
    }

    Future _checkGps() async {
      if (!await location.serviceEnabled()) {
        location.requestService();
      }
    }

    _getUserCurrentLocation() async {
      await _checkGps();
      final Geolocator geolocator = Geolocator()..forceAndroidLocationManager;

      await geolocator
          .getCurrentPosition(desiredAccuracy: LocationAccuracy.best)
          .then(
        (Position position) {
          if (position != null) {
            setState(
              () {
                usersCurrentLocationLat = position.latitude;
                usersCurrentLocationLong = position.longitude;
              },
            );
          }
        },
      ).catchError((e) {
        print(e);
      });
    }

    printRightAddress() async {
      await _getUserCurrentLocation();

      print("MY LOCATION COORDINATES=========:  " +
          usersCurrentLocationLat.toString() +
          " " +
          usersCurrentLocationLong.toString());//T*HIS ALWAYS PRINTS GPS LOCATION, I NEED TO GET DEFAULT VALUES IN CASE WHERE GPS IS NOT ENABLES OR LOCATION SERVICES ARE NOT ENABLED*
    }
  }
导入'dart:async';
进口“包装:颤振/cupertino.dart”;
进口“包装:颤振/材料.省道”;
导入“包:地理定位器/地理定位器.dart”;
将“包装:位置/位置.省道”作为loc导入;
类GetLocation扩展了StatefulWidget{
@凌驾
State createState()=>\u GetLocationState();
}
类_GetLocationState扩展状态{
最终地理定位器Geolocator=Geolocator()…forceAndroidLocationManager;
loc.Location位置=loc.Location();
双用户scurrentlocationlat=1.2921;
双用户currentlocationlong=30.8219;
@凌驾
void initState(){
super.initState();
printRightAddress();
}
@凌驾
小部件构建(构建上下文){
返回容器(
颜色:颜色,白色,
孩子:脚手架(
背景颜色:Colors.white,
主体:容器(
儿童:中心(
子项:文本(“TTTTT”),
//child:Text(usersCurrentLocationLat.toString()+
//     " , " +
//usersCurrentLocationLong.toString()),
),
),
),
);
}
Future\u checkGps()异步{
如果(!wait location.servicenabled()){
location.requestService();
}
}
_getUserCurrentLocation()异步{
等待检查GPS();
最终地理定位器Geolocator=Geolocator()…forceAndroidLocationManager;
等待地理定位器
.getCurrentPosition(所需精度:定位精度。最佳)
.那么(
(职位){
如果(位置!=null){
设定状态(
() {
usersCurrentLocationLat=位置纬度;
usersCurrentLocationLong=位置。经度;
},
);
}
},
).catchError((e){
印刷品(e);
});
}
printRightAddress()异步{
等待_getUserCurrentLocation();
打印(“我的位置坐标=+
usersCurrentLocationLat.toString()+
" " +
usersCurrentLocationLong.toString());/T*他总是打印GPS位置,如果GPS未启用或位置服务未启用,我需要获取默认值*
}
}