Flutter -Geolocator软件包不会多次调用GEOLOCATOR.GETCURRENTPOINTION()

发布于 2025-02-06 21:59:14 字数 804 浏览 6 评论 0原文

正如标题所说,我无法多次调用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不弃不离 2025-02-13 21:59:14

我假设您在iOS或MACOS上遇到这种行为。原因是Apple不允许对位置流进行多个活动调用。发生的事情是getLocation2方法关闭getLocation1呼叫当前运行的活动请求。

解决此问题的最简单方法是返回相同的Future如果第一个请求尚未完成:

  late Future<Position> _positionFuture;

  Future<Position> getLocation() async {
    if (_positionFuture != null && !_positionFuture!.isCompleted) {
      return _positionFuture;
    }

    _positionFuture = Geolocator.getCurrentPosition(
        desiredAccuracy: LocationAccuracy.medium,
      );
    return _positionFuture;
  }

  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();
  }

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 the getLocation1 call.

The easiest way to workaround this, is to return the same Future if the first request has not been finished yet:

  late Future<Position> _positionFuture;

  Future<Position> getLocation() async {
    if (_positionFuture != null && !_positionFuture!.isCompleted) {
      return _positionFuture;
    }

    _positionFuture = Geolocator.getCurrentPosition(
        desiredAccuracy: LocationAccuracy.medium,
      );
    return _positionFuture;
  }

  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();
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文