简体   繁体   English

Flutter 中的纬度和经度为空(获取当前位置)

[英]Latitude and Longitude is getting null in Flutter (Get Current Location)

I am using this plugin flutter_geofence to get current location latitude and longitude.我正在使用这个插件flutter_geofence来获取当前位置的纬度和经度。 Below is the sample dart code, problem i facing is inside the Geofence body i can get latitude and longitude but when i try to access outside it is getting null.下面是示例飞镖代码,我面临的问题是在Geofence体内,我可以获得纬度和经度,但是当我尝试在外部访问时,它变得空了。

return Scaffold(
          body: Container(
            child: GestureDetector(
                onTap: () {
                  var latitude;
                  var longitude;
                  Geofence.initialize();
                  Geofence.requestPermissions();
                  Geofence.getCurrentLocation().then((cd) {
                    latitude = cd!.latitude;
                    longitude = cd.longitude;
                  });
                  print(latitude); // getting null
                  print(longitude); // getting null
                },
                child: Center(child: Text('Test'))),
          ),
        );

DataService.class数据服务类

class DataService {
Future<List<RecommendedForYouData>?> makeRequestRecommendedForYou(
      BuildContext context) async {
var latitude;
var longitude;
Geofence.initialize();
                  Geofence.requestPermissions();
                  Geofence.getCurrentLocation().then((cd) {
                    latitude = cd!.latitude;
                    longitude = cd.longitude;
                  });
                  print(latitude); // getting null
                  print(longitude); // getting null
}
}

This problem happend because your variables are outside the function (Geofence.getCurrentLocation().then ...), if you put this variables inside {} probably you can get different value than null, but I recommend create a new function outside the build method.发生此问题是因为您的变量在函数之外 (Geofence.getCurrentLocation().then ...),如果您将此变量放在 {} 内,您可能会获得与 null 不同的值,但我建议在构建之外创建一个新函数方法。

  1. If you use a Stateful Widget:如果您使用有状态小部件:
  • Declare your variables in the top of your class.在类的顶部声明变量。
  • Use initState and call your methods inside (Geofence.initialize(), Geofence.requestPermissions(), Geofence.getCurrentLocation())使用 initState 并在内部调用您的方法(Geofence.initialize()、Geofence.requestPermissions()、Geofence.getCurrentLocation())
  • You can create a specific function outside the initState to call inside of your build method.你可以在 initState 之外创建一个特定的函数来在你的构建方法内部调用。
  • Inside your specific functions you need call the setState function to rebuild your UI.在您的特定函数中,您需要调用 setState 函数来重建您的 UI。

I recommend you use a state management like bloc or provider instead of setState.我建议你使用像 bloc 或 provider 这样的状态管理而不是 setState。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 根据Flutter中当前位置的经纬度获取完整地址详细信息 - Get full address details based on current location's latitude and longitude in Flutter Flutter 获取位置和经纬度 - Flutter fetch Location and Latitude and Longitude 在 flutter 中禁用 Internet 时获取我所在位置的纬度和经度 - get latitude and longitude of my location when internet is disable in flutter 如何使用点击地点的纬度和经度获取 Flutter 中地点的自动完成位置? - How to get autocomplete location of places in Flutter with Latitude and Longitude of tapped place? 如何在 google_maps_flutter 中获取当前的纬度和经度? - How to get current latitude & longitude in google_maps_flutter? 如何在颤振中获得纬度和经度 - How to get latitude and longitude in flutter 如何使用位置在颤动中转换纬度和经度 - how to convert latitude and longitude in flutter using location 获取数据快照到位置纬度经度 - get data snapshot to location latitude longitude 如何在颤振地图上获取标记纬度和经度? - How to get marker latitude and longitude on flutter map? 无法获取经纬度用户-Flutter - Unable to get latitude and longitude user - Flutter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM