I want to build a system that receives the position of my users at certain intervals when they press a button. I tried this with the getPositionStream management of the geolocater package and I had the same problem there. now when I try with timer, I have the same problem.
When I call the startTracking function with a button, I get the current position of the user for a certain period of time and even print it in firebase. But I can't stop this positioning process. As you can see in the function called stopTracking, if the variable named timer is not null, make it null and I want to stop the process with the .cancel() method, but whenever I check the timer value, it already has a null value.
I have no idea what to do, I have been dealing with this for hours.
class GpsService {
Timer? zamanlayici; // Sınıf seviyesinde zamanlayıcı
Future<void> startTracking(String reportId) async {
bool serviceEnabled;
LocationPermission permission;
zamanlayici = Timer.periodic(Duration(seconds: 10), (timer) async {
await _updateFirebase(reportId);
});
print('Zamanlayıcı başlatıldı.');
}
Future<void> _updateFirebase(String reportId) async {
try {
Position position = await Geolocator.getCurrentPosition();
FirebaseFirestore.instance
.collection('Reports')
.doc(reportId)
.collection('Coordinates')
.doc('last_location')
.set({
'latitude': position.latitude,
'longitude': position.longitude,
'timestamp': FieldValue.serverTimestamp(),
}, SetOptions(merge: true));
print(
'