title: Calculate distance between previous location and current location in Flutter
body: <p>I use the plugin <code>Geolocator</code> and I try to calculate the distance between my previous location and the current location. The only problem is that I don't know how to get the previous location.
  Is there any way to store it or to get it from <code>Geolocator</code> ? This functionality will help me to calculate the distance while I'm driving a car (at the moment I use iOS simulator feature to simulate a freeway drive).
  Here is my code:</p>
  <pre><code>import 'package:geolocator/geolocator.dart';
  import 'package:flutter/material.dart';
  import 'dart:async';
  
  void main() {
    runApp(MyApp());
  }
  
  class MyApp extends StatelessWidget {
    @override
    Widget build(BuildContext context) {
      return MaterialApp(
        title: 'Distance between Locations',
        debugShowCheckedModeBanner: false,
        theme: ThemeData(
          primarySwatch: Colors.blue,
        ),
        home: LocationPage(),
      );
    }
  }
  
  class LocationPage extends StatefulWidget {
    LocationPage({Key key}) : super(key: key);
  
    @override
    _LocationPageState createState() =&gt; _LocationPageState();
  }
  
  class _LocationPageState extends State&lt;LocationPage&gt; {
    Position _currentPosition;
    Position _previousPosition;
    StreamSubscription&lt;Position&gt; _positionStream;
    double _distance;
  
    @override
    void initState() {
      super.initState();
      _calculateDistance();
    }
  
    Future _calculateDistance() async {
      _positionStream = Geolocator.getPositionStream(
              distanceFilter: 10, desiredAccuracy: LocationAccuracy.high)
          .listen((Position position) async {
        if ((await Geolocator.isLocationServiceEnabled())) {
          Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.high)
              .then((Position position) {
            setState(() {
              _currentPosition = position;
              _distance = Geolocator.distanceBetween(
                  51.9021777, // how to get the previous Latitude position ?
                  -0.5257026, // how to get the previous Longitude position ?
                  position.latitude,
                  position.longitude);
            });
          }).catchError((err) {
            print(err);
          });
        } else {
          print(&quot;GPS is off.&quot;);
          showDialog(
              context: context,
              builder: (BuildContext context) {
                return AlertDialog(
                  content: const Text('Make sure your GPS is on in Settings !'),
                  actions: &lt;Widget&gt;[
                    FlatButton(
                        child: Text('OK'),
                        onPressed: () {
                          Navigator.of(context, rootNavigator: true).pop();
                        })
                  ],
                );
              });
        }
      });
    }
  
    @override
    void dispose() {
      super.dispose();
      _positionStream.cancel();
    }
  
    @override
    Widget build(BuildContext context) {
      return Scaffold(
        appBar: AppBar(
          title: Text('Location Manager'),
        ),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: &lt;Widget&gt;[
              Text(
                'Previous Latitude: ${_previousPosition?.latitude ?? '-'} \nPrevious Longitude: ${_previousPosition?.longitude ?? '-'}',
              ),
              SizedBox(height: 50),
              Text(
                'Current Latitude: ${_currentPosition?.latitude ?? '-'} \nCurrent Longitude: ${_currentPosition?.longitude ?? '-'}',
              ),
              SizedBox(height: 50),
              Text(
                  'Distance: ${_distance != null ? _distance.toStringAsFixed(2) : 0} meters.')
            ],
          ),
        ),
      );
    }
  }
  
  </code></pre>
  <p>Thanks for reading this !</p>