title: location in real time with flutter
body: <p>I am trying to capture the latitude and longitude in real time with my gps on my device however so far I am only getting the current longitude and latitude and it is not updating.</p>
  <p>What I want to do is that when the gps detects that the latitude or longitude has changed, they are updated on the screen</p>
  <p>additionally it is happening to me that when I start if I start the application and deny the permission the application breaks</p>
  <p>For this exercise I am using geolocator: ^ 5.3.2 + 2 I appreciate your help
  <a href="https://i.stack.imgur.com/MrtDY.png" rel="nofollow noreferrer">enter image description here</a></p>
  <p>Error <a href="https://i.stack.imgur.com/3fgl3.png" rel="nofollow noreferrer">enter image description here</a></p>
  <pre><code>import 'dart:async';
  import 'package:flutter/material.dart';
  import 'package:geolocator/geolocator.dart';
  
  void main() =&gt; runApp(App());
  
  class App extends StatefulWidget {
    @override
    _AppState createState() =&gt; _AppState();
  }
  
  class _AppState extends State&lt;App&gt; {
    String _locationMessage = &quot;&quot;;
  
    Future disponiblidad() async {
      var geoLocator = Geolocator();
      var status = await geoLocator.checkGeolocationPermissionStatus();
  
      if (status == GeolocationStatus.denied) {
        //pedirpermiso();
        // Llevar a la usuario a la configuración de permisos
        print('Llevar a la usuario a la configuración de permisos');
      } else if (status == GeolocationStatus.disabled) {
        //AppSettings.openLocationSettings();
  
        //Llevar a la usuario a la página de ubicación
        print('Llevar a la usuario a la página de ubicación');
      } else if (status == GeolocationStatus.restricted) {
        // Restringida
        print('Restringida');
      } else if (status == GeolocationStatus.unknown) {
        // Desconocida
        print('Desconocida');
      } else if (status == GeolocationStatus.granted) {
        //Permiso otorgado y ubicación habilitada
        print('Permiso otorgado');
        location();
      }
    }
  
    void pedirpermiso() async {
      final position = await Geolocator()
          .getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
      if (position == null) {}
    }
  
    void location() async {
      var geolocator = Geolocator();
      var locationOptions =
          LocationOptions(accuracy: LocationAccuracy.high, distanceFilter: 10);
  
      // ignore: unused_local_variable
      // ignore: cancel_subscriptions
      StreamSubscription&lt;Position&gt; positionStream = geolocator
          .getPositionStream(locationOptions)
          .listen((Position position) {
        setState(() {
          print(position == null
              ? 'Unknown'
              : position.latitude.toString() +
                  ', ' +
                  position.longitude.toString());
          _locationMessage =
              &quot;${position.latitude.toString()}, ${position.longitude.toString()}&quot;;
  
          //String hoa = positionStream;
        });
      });
    }
  
    @override
    void initState() {
      super.initState();
      // _checkGps();
      disponiblidad();
    }
  
    // This widget is the root of your application.
    @override
    Widget build(BuildContext context) {
      return MaterialApp(
          title: 'Flutter Demo',
          home: Scaffold(
              appBar: AppBar(title: Text(&quot;Location Services&quot;)),
              body: Align(
                child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: &lt;Widget&gt;[
                      Text(_locationMessage),
                      FlatButton(
                          onPressed: () {
                            // _getCurrentLocation();
                          },
                          color: Colors.green,
                          child: Text(&quot;Find Location&quot;))
                    ]),
              )));
    }
  }
  </code></pre>