I am really stuck on this part of the code (I am a beginner on Flutter). I want to display the stopwatch timer and total distance from geolocator, but the stopwatch just updates when "geolocation" is moving.
When button is pressed I am calling this:
void _toggleListening() {
if (_positionStreamSubscription == null) {
const LocationOptions locationOptions =
LocationOptions(accuracy: LocationAccuracy.best);
final Stream<Position> positionStream =
Geolocator().getPositionStream(locationOptions);
_positionStreamSubscription = positionStream.listen((Position position) {
setState(() {
_positions.add(position);
_allPositions = _positions;
_positionRealTime = position;
updatePinOnMap();
print('all lenght ${_allPositions.length}');
totalDistanceAux = 0.0;
for (var i = 0; i < _allPositions.length - 1; i++) {
totalDistanceAux = totalDistanceAux +
distance.as(
LengthUnit.Meter,
LatLng(_allPositions[i].latitude, _allPositions[i].longitude),
LatLng(_allPositions[i + 1].latitude,
_allPositions[i + 1].longitude),
);
}
totalDistance = totalDistanceAux;
print('total distance $totalDistance');
});
});
_positionStreamSubscription.pause();
_stopwatch.stop();
} else {
setState(() {
if (_positionStreamSubscription.isPaused == true) {
_positionStreamSubscription.resume();
_stopwatch.start();
}
});
}
}
and I am displaying with FutureBuilder:
FutureBuilder<GeolocationStatus>(
future: Geolocator().checkGeolocationPermissionStatus(),
builder: (BuildContext context,
AsyncSnapshot<GeolocationStatus> snapshot) {
The distance and timer is working perfectly, the only problem is that timer just updates when there is some movement. This is my first question at StackOverflow, sorry if I did something wrong. Thanks!
If you want to rebuild a widget periodically, add the timer_builder package and use it like that :
TimerBuilder.periodic(Duration(seconds: 1),
builder: (context) {
return YourWidget();
}
)