Home > Enterprise >  Flutter Firebase: How to compare two fields' number value from firebase
Flutter Firebase: How to compare two fields' number value from firebase

Time:11-25

Basically, I want to compare the number value in 'Latitude' field and 'CurrLatitude' field (they are both saved in firebase), if they are not matched, the alarm will be triggered.

As you can see from line 186, I realised that the comparison is within the 'String' but not the 'number' value. So, I wonder how should I solve this?

Flutter code 'Latitude' and 'CurrLatitude' in Firebase

Future<void> _listenLocation() async {
    _locationSubscription = location.onLocationChanged.handleError((onError) {
      print(onError);
      _locationSubscription?.cancel();
      setState(() {
        _locationSubscription = null;
      });
    }).listen((loc.LocationData currentlocation) async {
      await FirebaseFirestore.instance.collection('users').doc(user!.uid).set({
        'currlatitude': currentlocation.latitude,
        'currlongitude': currentlocation.longitude,
      }, SetOptions(merge: true));
    });
    if (('latitude') == ('currLatitude')) {
                    if (('longitude') == ('currLongitude')) {
                      AndroidAlarmManager.periodic(
                    Duration(seconds: 0), alarmId, stopAlarm);
                    }
                    else if (('longitude') != ('currLongitude')) {
                      AndroidAlarmManager.periodic(
                    Duration(seconds: 0), alarmId, fireAlarm);
                    }
                  }
    else if (('latitude') != ('currLatitude')) {
                    AndroidAlarmManager.periodic(
                    Duration(seconds: 2), alarmId, fireAlarm);
                  }
                    }
                }

CodePudding user response:

In statement like this you are comparing constant strings:

if (('latitude') == ('currLatitude')) {

The string 'latitude' is never going to equal to the string 'currLatitude'.

What you probably want to do is:

  1. Read the document where you have values for these fields with get().
  2. Then get those field values from the document.
  3. Compare the resulting field values in your code instead.

CodePudding user response:

I think you have to compare two varibales which have the value of lat & long, but comparing String variable isn't logic. Then you can do the goal of what you want.

  • Related