Getx oninit dont replace values – Geolocator

Issue

I was testing a code on my Android device but in the replacement part of the code in the terminal it prints the previous values and not the values changed in the getPosition().

CODE WITH GEOLOCATOR

import 'package:geolocator/geolocator.dart';
import 'package:get/get.dart';

import 'package:get/get.dart';

class GlobalController extends GetxController {
//variables
  final RxBool _isloading = true.obs;
  final RxDouble _latitud = 0.0.obs;
  final RxDouble _longitud = 0.0.obs;
  RxBool checkLoading() => _isloading;
  RxDouble getLatitude() => _latitud;
  RxDouble getlongitude() => _longitud;
  @override
  void onInit() {
    getLocation();
    super.onInit();
  }

  getLocation() async {
    bool isServiceEnabled;
    LocationPermission locationPermission;
    isServiceEnabled = await Geolocator.isLocationServiceEnabled();
    if (!isServiceEnabled) {
      return Future.error("GPS no disponible");
    }

    locationPermission = await Geolocator.checkPermission();

    if (locationPermission == LocationPermission.deniedForever) {
      return Future.error("La aplicación nunca tendrá los permisos");
    } else if (locationPermission == LocationPermission.denied) {
      locationPermission = await Geolocator.requestPermission();
      if (locationPermission == LocationPermission.denied) {
        return Future.error("La aplicación no tiene los permisos");
      }
    }

    return Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.best)
        .then((value) => {
              _latitud.value = value.longitude,
              _longitud.value = value.longitude,
              _isloading.value = false
            });
  }
}

widget where I call geolocator

import 'package:flutter/material.dart';
import 'package:geocoding/geocoding.dart';
import 'package:get/get.dart';
import '../controller/global_controller.dart';

class HeaderWidget extends StatefulWidget {
  const HeaderWidget({super.key});

  @override
  State<HeaderWidget> createState() => _HeaderWidgetState();
}

class _HeaderWidgetState extends State<HeaderWidget> {
  String city = "Hola Mundo";
  final GlobalController globalController =
      Get.put(GlobalController(), permanent: true);
  @override
  void initState() {
    getAddress();
    super.initState();
  }

  getAddress() async {
    double value = GlobalController().getLatitude().value;
    city = "Reemplazado";
    print(value);
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Container(
          child: Text(city),
        ),
      ],
    );
  }
}

At the beginning I thought that it could be an error in the last return, so I changed the values at the beginning of the class and it still did not change. Similar when I changed the values in onInit()

Solution

After assigning the value, refresh the variables

    return Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.best)
    .then((value) => {
          _latitud.value = value.longitude,
          _longitud.value = value.longitude,
          _isloading.value = false

          //<<---Try adding this--->>
          _latitud.refresh(),
          _longitud.refresh(),
          _isloading.refresh(),
        });

Answered By – Vinay Baise

Answer Checked By – Dawn Plyler (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *