Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getLocation() never completes while another getLocation() call is pending #281

Closed
gillesroux opened this issue Feb 13, 2020 · 7 comments
Closed

Comments

@gillesroux
Copy link

The getLocation() API does not support concurrent invocations and results in a future that never completes. It's preferable to avoid concurrent invocation but different parts of the app may be using the location plugin making it hard to avoid.

Here is a code sample demonstrating the issue. 'done' is never printed.
print('start');
final location1 = location.getLocation();
final location2 = location.getLocation();
await location1;
await location2;
print('done');

This behavior seems to be due to the fact that the plugin overwrites the method call result:
https://github.com/Lyokone/flutterlocation/blob/master/android/src/main/java/com/lyokone/location/LocationPlugin.java#L152

One workaround is to introduce a wrapper that serializes getLocation calls:
final lock = Lock();
Future getLocation() async =>
lock.synchronized(() async => location.getLocation());

Tested on Android 10 on a Pixel 3.

@Lyokone
Copy link
Owner

Lyokone commented Feb 17, 2020

Hey ! The getLocation is in fact not supporting concurrent calls.
I'll evaluate if a lock, or an Array of getLocationResults is better for solving this :)

@stale
Copy link

stale bot commented Mar 18, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the inactive label Mar 18, 2020
@halldorlogi
Copy link

Any news on this?

@stale stale bot removed the inactive label Mar 19, 2020
@Lyokone
Copy link
Owner

Lyokone commented Mar 26, 2020

Hey ! I did not had time to tackle this yet. Sorry for the delay :/

@Kristijan505
Copy link

Hi,

we had the exact same issue while trying to get users location multiple times.

Our solution was to make a variable named lastLocation, where we would write users location every time that getLocation() has succeeded and set a timeout on getter with:

LocationData lastLocation; - as a global variable

Location().getLocation().timeout(Duration(seconds: 1), onTimeout: () => lastLocation);

I hope it helps.

@Dorbmon
Copy link

Dorbmon commented Apr 7, 2020

Hi,

we had the exact same issue while trying to get users location multiple times.

Our solution was to make a variable named lastLocation, where we would write users location every time that getLocation() has succeeded and set a timeout on getter with:

LocationData lastLocation; - as a global variable

Location().getLocation().timeout(Duration(seconds: 1), onTimeout: () => lastLocation);

I hope it helps.

Thanks for your help.You saved the day haha

@stale
Copy link

stale bot commented May 7, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the inactive label May 7, 2020
@stale stale bot closed this as completed May 14, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants