flutter - 如何返回 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>' */
/* =============================== */
解决方案
Future<Position> _getLocation() async {
return await Geolocator()
.getCurrentPosition(desiredAccuracy: LocationAccuracy.low);
}
Position userPosition = await _getLocation();
print(userPosition);
推荐阅读
- amazon-web-services - 允许 Stripe webhook 访问 AWS EC2 实例
- ubuntu - 如何在 vscode ubuntu 20.04 中显示表情符号?
- node.js - 无法通过匹配 _id 更新 MongoDB 集合
- ssl - Heroku 的变化。如何在免费的 Heroku dynos 上继续使用免费的 Cloudflare SSL?
- npm - npm run deploy 无法从 ./node_modules/nanoid/index.browser.js:34 缩小代码
- python - 在文本文件中搜索特定单词
- android - 您能否创建应用的免费版本和付费版本,但在用户的设备上只拥有其中一个?
- ios - 如何管理多个 AVAudioPCMBuffer 进行播放?
- javascript - 忽略javascript中的重音字母
- c++ - 可以 std::vector
::iterator 实现其 deref 运算符以返回对 _Bit_reference 的引用?