I am using workmanager and geolocator package and trying to access user location in background and also save it into firestore but it doesn’t work
Here is the code
@pragma('vm:entry-point')
void callbackDispatcher() {
Workmanager().executeTask((task, inputData) async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
DartPluginRegistrant.ensureInitialized();
switch (task) {
case 'adminTask':
await performAdminTask();
break;
case 'userTask':
await performUserTask();
break;
default:
print('Unknown task');
break;
}
return Future.value(true);
});
}
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
Workmanager().initialize(
callbackDispatcher,
isInDebugMode: true,
);
runApp(const MyApp());
}
Here is the performAdmintask function
import 'dart:async';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/foundation.dart';
import 'package:geolocator/geolocator.dart';
Future<void> performAdminTask() async {
late LocationSettings locationSettings;
if (defaultTargetPlatform == TargetPlatform.android) {
locationSettings = AndroidSettings(
accuracy: LocationAccuracy.high,
forceLocationManager: true,
intervalDuration: const Duration(seconds: 10),
foregroundNotificationConfig: const ForegroundNotificationConfig(
notificationText:
"Example app will continue to receive your location even when you aren't using it",
notificationTitle: "Running in Background",
enableWakeLock: true,
));
} else if (defaultTargetPlatform == TargetPlatform.iOS ||
defaultTargetPlatform == TargetPlatform.macOS) {
locationSettings = AppleSettings(
accuracy: LocationAccuracy.high,
activityType: ActivityType.fitness,
pauseLocationUpdatesAutomatically: true,
// Only set to true if our app will be started up in the background.
showBackgroundLocationIndicator: false,
);
} else {
locationSettings = const LocationSettings(
accuracy: LocationAccuracy.high,
);
}
Geolocator.getPositionStream(locationSettings: locationSettings)
.listen((event) async {
await saveLocationToFirestore(event);
});
}
Future<void> saveLocationToFirestore(Position position) async {
try {
const userRole = 'Admin';
final docRef = FirebaseFirestore.instance
.collection(userRole)
.doc(FirebaseAuth.instance.currentUser!.uid);
final docData = await docRef.get();
Map<String, dynamic> map = docData.data() as Map<String, dynamic>;
if (map.containsKey('latitude') && map.containsKey('longitude')) {
docRef.update({
'latitude': position.latitude,
'longitude': position.longitude,
'timestamp': FieldValue.serverTimestamp(),
});
} else {
docRef.set({
'latitude': position.latitude,
'longitude': position.longitude,
'timestamp': FieldValue.serverTimestamp(),
}, SetOptions(merge: true));
}
} catch (e) {
print('Error saving location to Firestore: $e');
}
}
and here i register task
`
Workmanager().registerOneOffTask(
'adminTaskId',
'adminTask',
);
What is the problem i dont understand i am using workmanager 0.5.2 and geolocator 11.1.0.