最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

firebase - How can I stop the timer function in the flutter? - Stack Overflow

programmeradmin6浏览0评论

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(
          '
发布评论

评论列表(0)

  1. 暂无评论