-
Notifications
You must be signed in to change notification settings - Fork 2
Description
Environment:
- Flet Version: 0.27.6
- flet-geolocator Version: 0.1.0)
- Platform: Android version 15
- Android Version: 15
- Device: Google Pixel 6 Pro
- Python Version:. 3.12.8
Bug Description:
When using the Geolocator control on Android, the listener associated with the on_position_change event does not appear to activate correctly within the same application session where location permissions (WHILE_IN_USE or ALWAYS) are granted for the first time via request_permission_async.
Steps to Reproduce:
- Install your sample Flet application using flet-geolocator on an Android device where it does not currently have location permissions.
- Run the application, grant permissions
- You can use on demand features, like get current position
- Walk around, notice that no Position Change events fire.
- Restart the application (you will probably see Position Change immediately)
- Walk around to see the event fire after the restart.
Expected Behavior:
The on_position_change callback should start receiving location updates immediately (or shortly after) location permissions are successfully granted within the same application session, without requiring an app restart.
Analysis / Likely Cause:
Based on inspecting the geolocator.dart code (specifically the _GeolocatorControlState), it appears the subscription to the native Geolocator.getPositionStream happens within the didChangeDependencies() lifecycle method. This method runs early during widget initialization.
It seems that if permissions are not already granted when didChangeDependencies() first runs, the stream subscription might fail to set up the native listener correctly, even if the Dart StreamSubscription object is created later (or if the check widget.control.attrBool("onPositionChange", false)! was initially false). Granting permission later via requestPermission does not re-trigger didChangeDependencies or the native listener setup for the continuous stream. Restarting the app works because the permissions exist before didChangeDependencies runs, allowing the native listener to be registered successfully during initialization.
Attempts to work around this in Python by destroying/recreating the Geolocator control or by calling a custom Dart method (via invoke_method_async) to explicitly start the stream after permission grant also failed to activate the listener without an app restart, further suggesting the limitation is in how the underlying native listener is initialized or activated dynamically by the geolocator package.
Suggested Improvement:
Ideally, the GeolocatorControl should be able to reliably start the background location listener immediately after permissions are granted dynamically within the same session. This might involve modifying the Dart code to explicitly re-initialize or re-register the native listener when triggered by a method call from Python after a successful permission request.
Thank you for Flet, just stumbled upon it this past weekend. It's great!