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?
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:
- Read the document where you have values for these fields with
get()
. - Then get those field values from the document.
- 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.