{"record":{"id":"6257c84e21254d9b","repo":"immich-app/immich","slug":"user-must-be-logged-in-to-access-map","errorCode":null,"errorMessage":"User must be logged in to access map","messagePattern":"User must be logged in to access map","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"mobile/lib/providers/infrastructure/map.provider.dart","lineNumber":12,"sourceCode":"import 'package:hooks_riverpod/hooks_riverpod.dart';\nimport 'package:immich_mobile/domain/services/map.service.dart';\nimport 'package:immich_mobile/presentation/widgets/map/map.state.dart';\nimport 'package:immich_mobile/providers/infrastructure/db.provider.dart';\nimport 'package:immich_mobile/providers/infrastructure/timeline.provider.dart';\nimport 'package:immich_mobile/providers/user.provider.dart';\n\nfinal mapServiceProvider = Provider<MapService>(\n  (ref) {\n    final user = ref.watch(currentUserProvider);\n    if (user == null) {\n      throw Exception('User must be logged in to access map');\n    }\n\n    final users = ref.watch(mapStateProvider).withPartners\n        ? ref.watch(timelineUsersProvider).valueOrNull ?? [user.id]\n        : [user.id];\n\n    final mapFactory = MapFactory(mapRepository: ref.watch(driftProvider).mapRepository);\n    final mapService = mapFactory.remote(users, ref.watch(mapStateProvider).toOptions());\n    return mapService;\n  },\n  // Empty dependencies to inform the framework that this provider\n  // might be used in a ProviderScope\n  dependencies: const [],\n);\n","sourceCodeStart":1,"sourceCodeEnd":27,"githubUrl":"https://github.com/immich-app/immich/blob/e55ac299a4ec7cb372e35dbf2c6c05ee9ce77f6c/mobile/lib/providers/infrastructure/map.provider.dart#L1-L27","documentation":"Thrown by the top-level mapServiceProvider Provider when currentUserProvider is null. MapService construction needs a user id (for user-scoped map queries), so reading it in an unauthenticated state throws at provider initialization. Any widget watching mapServiceProvider without a logged-in user hits this.","triggerScenarios":"Watching mapServiceProvider while currentUserProvider is null — during app startup before auth resolves, after logout, or after token expiry cleared the user.","commonSituations":"Map tab rendered before login completes; session invalidated server-side while the map screen is open; auth restoration racing with the first frame that watches the provider.","solutions":["Gate map UI behind authenticated route guards so the provider is only read when a user exists.","Make mapServiceProvider an AsyncProvider (or FutureProvider) that waits for auth state instead of throwing synchronously.","Re-authenticate (token refresh/login) when this error is caught, then rebuild.","Return null/safe fallback from dependent widgets when the user is absent."],"exampleFix":"// before\nfinal user = ref.watch(currentUserProvider);\nif (user == null) {\n  throw Exception('User must be logged in to access map');\n}\n// after\nfinal user = ref.watch(currentUserProvider);\nif (user == null) {\n  return null; // in a Provider<Coordinates?> / async variant; callers handle null\n}","handlingStrategy":"type-guard","validationCode":"final user = ref.read(currentUserProvider);\nif (user == null) {\n  // do not watch mapServiceProvider; route to login\n  return;\n}\nfinal mapService = ref.read(mapServiceProvider);","typeGuard":"bool canUseMapService(User? u) => u != null && u.id.isNotEmpty;","tryCatchPattern":"try {\n  final mapService = ref.watch(mapServiceProvider);\n  // use service\n} on Exception catch (e) {\n  if (e.toString().contains('User must be logged in')) {\n    context.go('/login');\n  } else {\n    rethrow;\n  }\n}","preventionTips":["Guard widgets that watch mapServiceProvider behind auth checks","Convert to an async/nullable provider to avoid sync throws","Refresh sessions before they expire","Never read user-scoped providers during pre-login frames"],"tags":["authentication","flutter","riverpod","map"],"backgroundTag":"authentication-required","analyzedSha":"e55ac299a4ec7cb372e35dbf2c6c05ee9ce77f6c","analyzedAt":"2026-09-15T07:20:19.675Z","contentChangedAt":"2026-09-15T07:20:19.675Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}