I am building an app where i use Flutter Background service to update user location as the following
@override
@pragma('vm:entry-point')
Future<void> shareTripLocation(Driver currentDriver, bool isActive,
Position? userCurrentPosition) async {
driverObject = await getCurrentUser();
driverId = currentDriver.userId;
isActiveRepo = isActive;
SharedPreferences preferences = await SharedPreferences.getInstance();
await preferences.setString("driverId", driverId.toString());
final service = FlutterBackgroundService();
if (!await service.isRunning()) {
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
if (Platform.isAndroid) {
const AndroidNotificationChannel channel = AndroidNotificationChannel(
'myService', // id
'MY FOREGROUND SERVICE', // title
description:
'This channel is used for important notifications.', // description
importance:
Importance.low, // importance must be at low or higher level
);
final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
await flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin>()
?.createNotificationChannel(channel);
}
// Configure background service for foreground and background execution
try {
await service.configure(
androidConfiguration: AndroidConfiguration(
// Execute tasks in a separate isolate (optional for better performance)
onStart: onStart,
autoStart: true,
isForegroundMode: true,
notificationChannelId: 'myService',
initialNotificationTitle: 'Sharing Location',
initialNotificationContent: 'Background service is running',
//foregroundServiceType: AndroidForegroundType.location,
foregroundServiceNotificationId: 888),
iosConfiguration: IosConfiguration(
autoStart: true,
onForeground: onStart,
),
);
} catch (e) {
log(e.toString());
}
driverId = (await getCurrentUser()).userId;
service.startService();
// Register plugins (only necessary for pre-Flutter 3.0)
if (Platform.isAndroid || Platform.isIOS) {
// var initializationSettingsAndroid =
// AndroidInitializationSettings('@mipmap/ic_launcher');
await FlutterLocalNotificationsPlugin().initialize(
const InitializationSettings(
iOS: DarwinInitializationSettings(),
android: AndroidInitializationSettings('@mipmap/ic_launcher'),
// Replace with your notification icon
),
);
}
DartPluginRegistrant.ensureInitialized();
}
log('setting');
}
@pragma('vm:entry-point')
static void onStart(ServiceInstance service) async {
log('start function');
log(driverId ?? 'non2');
// Obtain user location updates
updateDriverLiveLocation(isActiveRepo, driverObject, service);
// Listen for service termination and cancel location updates
service.on('stop').listen((event) async {
service.stopSelf();
});
}
static updateDriverLiveLocation(
bool isActive, Driver driver, ServiceInstance service) async {
// await Firebase.initializeApp();
// final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
// FlutterLocalNotificationsPlugin();
// SharedPreferences preferences = await SharedPreferences.getInstance();
// String? uid = preferences.getString("driverId");
// Timer.periodic(const Duration(seconds: 3), (timer) async {
// try {
// Position position = await Geolocator.getCurrentPosition(
// desiredAccuracy: LocationAccuracy.high);
// preferences.reload();
// Trip trip = Trip.fromEntity(TripEntity.fromDocument(json.decode(preferences.getString('trip').toString())));
// log('new location');
// await FirebaseFirestore.instance
// .collection("trips")
// .doc(trip.tripId)
// .update({'tripCurrentLocation': Location(latitude: position.latitude, langitude: position.longitude).toEntity().toDocument()});
// } catch (e) {
// log(e.toString());
// }
// });
}
in the other hand i implemented a usual Google Map screen that listen to firebase document update, it works fine and get updated with every firebase change
but if i run the background service the following error appears with every map state change or background code execution
E/flutter ( 7742): [ERROR:flutter/runtime/dart_vm_initializer.cc(40)] Unhandled Exception: Unknown map ID 0
E/flutter ( 7742): #0 GoogleMapsFlutterAndroid._hostApi (package:google_maps_flutter_android/src/google_maps_flutter_android.dart:87:7)
E/flutter ( 7742): #1 GoogleMapsFlutterAndroid.updateClusterManagers (package:google_maps_flutter_android/src/google_maps_flutter_android.dart:341:12)
E/flutter ( 7742): #2 GoogleMapController._updateClusterManagers (package:google_maps_flutter/src/controller.dart:118:10)
E/flutter ( 7742): #3 _GoogleMapState._updateClusterManagers (package:google_maps_flutter/src/google_map.dart:424:26)
E/flutter ( 7742): <asynchronous suspension>
E/flutter ( 7742):
E/flutter ( 7742): [ERROR:flutter/runtime/dart_vm_initializer.cc(40)] Unhandled Exception: Unknown map ID 0
E/flutter ( 7742): #0 GoogleMapsFlutterAndroid._hostApi (package:google_maps_flutter_android/src/google_maps_flutter_android.dart:87:7)
E/flutter ( 7742): #1 GoogleMapsFlutterAndroid.updateMarkers (package:google_maps_flutter_android/src/google_maps_flutter_android.dart:245:12)
E/flutter ( 7742): #2 GoogleMapController._updateMarkers (package:google_maps_flutter/src/controller.dart:106:10)
E/flutter ( 7742): #3 _GoogleMapState._updateMarkers (package:google_maps_flutter/src/google_map.dart:417:26)
E/flutter ( 7742): <asynchronous suspension>
E/flutter ( 7742):
E/flutter ( 7742): [ERROR:flutter/runtime/dart_vm_initializer.cc(40)] Unhandled Exception: Unknown map ID 0
E/flutter ( 7742): #0 GoogleMapsFlutterAndroid._hostApi (package:google_maps_flutter_android/src/google_maps_flutter_android.dart:87:7)
E/flutter ( 7742): #1 GoogleMapsFlutterAndroid.updatePolygons (package:google_maps_flutter_android/src/google_maps_flutter_android.dart:257:12)
E/flutter ( 7742): #2 GoogleMapController._updatePolygons (package:google_maps_flutter/src/controller.dart:129:10)
E/flutter ( 7742): #3 _GoogleMapState._updatePolygons (package:google_maps_flutter/src/google_map.dart:431:26)
E/flutter ( 7742): <asynchronous suspension>
E/flutter ( 7742):
E/flutter ( 7742): [ERROR:flutter/runtime/dart_vm_initializer.cc(40)] Unhandled Exception: Unknown map ID 0
E/flutter ( 7742): #0 GoogleMapsFlutterAndroid._hostApi (package:google_maps_flutter_android/src/google_maps_flutter_android.dart:87:7)
E/flutter ( 7742): #1 GoogleMapsFlutterAndroid.updatePolylines (package:google_maps_flutter_android/src/google_maps_flutter_android.dart:271:12)
E/flutter ( 7742): #2 GoogleMapController._updatePolylines (package:google_maps_flutter/src/controller.dart:140:10)
E/flutter ( 7742): #3 _GoogleMapState._updatePolylines (package:google_maps_flutter/src/google_map.dart:438:26)
E/flutter ( 7742): <asynchronous suspension>
E/flutter ( 7742):
E/flutter ( 7742): [ERROR:flutter/runtime/dart_vm_initializer.cc(40)] Unhandled Exception: Unknown map ID 0
E/flutter ( 7742): #0 GoogleMapsFlutterAndroid._hostApi (package:google_maps_flutter_android/src/google_maps_flutter_android.dart:87:7)
E/flutter ( 7742): #1 GoogleMapsFlutterAndroid.updateCircles (package:google_maps_flutter_android/src/google_maps_flutter_android.dart:289:12)
E/flutter ( 7742): #2 GoogleMapController._updateCircles (package:google_maps_flutter/src/controller.dart:151:10)
E/flutter ( 7742): #3 _GoogleMapState._updateCircles (package:google_maps_flutter/src/google_map.dart:445:26)
E/flutter ( 7742): <asynchronous suspension>
E/flutter ( 7742):
E/flutter ( 7742): [ERROR:flutter/runtime/dart_vm_initializer.cc(40)] Unhandled Exception: Unknown map ID 0
E/flutter ( 7742): #0 GoogleMapsFlutterAndroid._hostApi (package:google_maps_flutter_android/src/google_maps_flutter_android.dart:87:7)
E/flutter ( 7742): #1 GoogleMapsFlutterAndroid.updateHeatmaps (package:google_maps_flutter_android/src/google_maps_flutter_android.dart:301:12)
E/flutter ( 7742): #2 GoogleMapController._updateHeatmaps (package:google_maps_flutter/src/controller.dart:162:10)
E/flutter ( 7742): #3 _GoogleMapState._updateHeatmaps (package:google_maps_flutter/src/google_map.dart:453:18)
E/flutter ( 7742): <asynchronous suspension>
E/flutter ( 7742):
E/flutter ( 7742): [ERROR:flutter/runtime/dart_vm_initializer.cc(40)] Unhandled Exception: Unknown map ID 0
E/flutter ( 7742): #0 GoogleMapsFlutterAndroid._hostApi (package:google_maps_flutter_android/src/google_maps_flutter_android.dart:87:7)
E/flutter ( 7742): #1 GoogleMapsFlutterAndroid.updateTileOverlays (package:google_maps_flutter_android/src/google_maps_flutter_android.dart:323:12)
E/flutter ( 7742): #2 GoogleMapController._updateTileOverlays (package:google_maps_flutter/src/controller.dart:173:10)
E/flutter ( 7742): #3 _GoogleMapState._updateTileOverlays (package:google_maps_flutter/src/google_map.dart:462:26)
E/flutter ( 7742): <asynchronous suspension>
E/flutter ( 7742):
i commented the firebase location update code to check if the code is causing the error or the error shows up once the background service is run
and even the background service code is empty once running the background service the map cannot be updated and show the previous error
obviously the map is running in foreground
thanks for giving time to help 🙂