Subscription¶
Handle returned by every BackgroundGeolocation.on* event-listener method.
Subscription exposes a single remove method used to stop
listening to an event and free associated resources. Always call remove()
when a listener is no longer needed to prevent memory leaks.
Removing an event-listener¶
Subscription subscription = BackgroundGeolocation.onLocation((Location location) {
print('[onLocation] ${location}');
});
// Later, when the listener is no longer needed:
subscription.remove();
Managing multiple subscriptions¶
Collect subscriptions in an array and remove them all at once — for example when a view is destroyed.
List<dynamic> subscriptions = [];
void addListeners() {
subscriptions.addAll([
BackgroundGeolocation.onLocation((Location location) {
print('[onLocation] ${location}');
}),
BackgroundGeolocation.onMotionChange((MotionChangeEvent event) {
print('[onMotionChange] ${event}');
}),
BackgroundGeolocation.onEnabledChange((enabled) {
print('[onEnabledChange] ${enabled}');
}),
]);
}
void removeListeners() {
for (final sub in subscriptions) {
sub.remove();
}
subscriptions.clear();
}