首页 > 解决方案 > 如何返回 flotter 地理定位器位置值?(编程语言:飞镖)

问题描述

我正在使用颤振制作应用程序。

我正在尝试使用地理定位器库将位置值保存到变量中,但由于以下错误,我无法打印该值。

请查看下面的代码并告诉我如何将返回位置值保存在变量中。谢谢你。

/* Convert to location using Geolocator */
Future<Position> _getLocation() async {
    return Geolocator()
        .getCurrentPosition(desiredAccuracy: LocationAccuracy.low)
        .then((location) {
      return location;
    });
  }

/* run app */

  @override
  Widget build(BuildContext context) {
    _getUser();
    print(_getLocation());
    
    - omit below -
    
/*  =============================== */
/*  Output value when the app runs  */
/*  Instance of 'Future<Position>'  */
/*  =============================== */

标签: flutterdart

解决方案


   Future<Position> _getLocation() async {
          return await Geolocator()
              .getCurrentPosition(desiredAccuracy: LocationAccuracy.low);
        }

Position userPosition = await _getLocation();
print(userPosition);

推荐阅读