daytona
daytona

Reputation: 1

Flutter app not update automatically position with geolocator and geocoding

I'm building an app in flutter and in android, and i have an issue since further days, it's not updating automatically the position. here the logs: W/Activity(23615): Can request only one set of permissions at a time I/Geolocator(23615): The grantResults array is empty. This can happen when the user cancels the permission request

Here is the code:

geolocator
    if (serviceEnabled) {
      try {
        Position position = await Geolocator.getCurrentPosition(
         desiredAccuracy: LocationAccuracy.best);

        final String currentTimeZone = await FlutterTimezone.getLocalTimezone();
        prayerTimeAPI(position.latitude, position.longitude, currentTimeZone);
        getAddress(position.latitude, position.longitude);
        update();

      } catch (e) {
        await prefs.setBool("isLocationDenied", true);
        bool? storedFontSize = prefs.getBool("isLocationDenied");
        if (storedFontSize != null) {
          isLocationDenied.value = storedFontSize;
        }

geocoding
// Get Address Function====>
  getAddress(double lat, double long) async {
    try {
      List<Placemark> addressList = await placemarkFromCoordinates(lat, long);

      final callAddress = addressList.first;
      currentAddress.value = callAddress.locality!;
      update();
    } catch (e) {
      print("Location not found");
    }
  }
ui implementation

    // address
        Expanded(
                child: Text(
             mosqueSettingsController
                                                  .mosqueSettingsApiData!
          .data!
          .automaticPayerTime ==
           true
 ? packagePrayerTimeController
        .currentAddress
           .value
         : mosqueSettingsController
            .mosqueSettingsApiData!
             .data!
               .mosqueAddress
                    .toString(),
                    style: robotoRegular.copyWith(
                   fontSize: Dimensions.FONT_SIZE_DEFAULT,
                                        color: Theme.of(context).hintColor,
                                  ),
                                 ),
                               ),
                            ],
                          ),
                       ],
                  ),

I want the position to update automatically in the ui app but it only works when i click in another function of the app or performing refresh. I could provide more info if needed. Thanks

Upvotes: 0

Views: 94

Answers (1)

Data part, using Geolocator: First of all, make sure permission is granted:

var permission = await Geolocator.checkPermission();
if (permission != LocationPermission.always && permission != LocationPermission.whileInUse) {
permission = await Geolocator.requestPermission();

Also check accuracy if needed.

Second: You need to make one request before listening:

await Geolocator.getCurrentPosition();

Get steam:

Stream<Position> positionStream = Geolocator.getPositionStream(
locationSettings: LocationSettings(timeLimit: Duration(seconds: 5)));

Handle events:

positionStream.listen((position) {...

If it not help, add logger, and make sure problem is on data part, not on ui. If you have ui updating issue, write your ui implementation.

Upvotes: 0

Related Questions