Flutter -Geolocator软件包不会多次调用GEOLOCATOR.GETCURRENTPOINTION()
正如标题所说,我无法多次调用Geolocator.getCurrentPosition()。我已经用包装团队记录了问题,但是我想知道这是否是我做错了什么。
Future getLocation() async {
var currentLocation;
try {
currentLocation = await Geolocator.getCurrentPosition(
desiredAccuracy: LocationAccuracy.medium,
);
} catch (e) {
currentLocation = null;
}
return currentLocation;
}
void getLocation1() async {
var location = await getLocation();
print(location);
print("First");
}
void getLocation2() async {
var location = await getLocation();
print(location);
print("Second");
}
@override
void initState() {
super.initState();
getLocation1();
getLocation2();
}
控制台打印“第二”但不是“第一”。当我删除getLocation2()调用时,控制台打印为“首先”。它们无法在同一框架中被调用,我不知道为什么会更改。我做错了吗?
As the title says, I'm not being able to call Geolocator.getCurrentPosition() more than once. I have logged the issue with the package team, but I'm wondering if it's something I'm doing wrong.
Future getLocation() async {
var currentLocation;
try {
currentLocation = await Geolocator.getCurrentPosition(
desiredAccuracy: LocationAccuracy.medium,
);
} catch (e) {
currentLocation = null;
}
return currentLocation;
}
void getLocation1() async {
var location = await getLocation();
print(location);
print("First");
}
void getLocation2() async {
var location = await getLocation();
print(location);
print("Second");
}
@override
void initState() {
super.initState();
getLocation1();
getLocation2();
}
The console prints "Second" but not "First". When I remove the getLocation2() call, the console prints "First". They are not able to be called in the same frame, and I don't know why this changed. Am I doing anything wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我假设您在iOS或MACOS上遇到这种行为。原因是Apple不允许对位置流进行多个活动调用。发生的事情是
getLocation2
方法关闭getLocation1
呼叫当前运行的活动请求。解决此问题的最简单方法是返回相同的
Future
如果第一个请求尚未完成:I am assuming you are experiencing this behavior on iOS or macOS. The reason is that Apple doesn't allow multiple active calls to the location stream. What happens is that the
getLocation2
method closes the active request currently running by thegetLocation1
call.The easiest way to workaround this, is to return the same
Future
if the first request has not been finished yet: