@@ -325,18 +325,28 @@ class AppBloc extends Bloc<AppEvent, AppState> {
325325
326326 // If a new user is present, handle their data.
327327 if (newUser != null ) {
328- // In demo mode, ensure user-specific data is initialized BEFORE fetching.
328+ // In demo mode, ensure essential user-specific data (settings,
329+ // preferences, and the user object itself in the data client)
330+ // are initialized if they don't already exist. This prevents
331+ // NotFoundException during subsequent reads.
329332 if (_environment == local_config.AppEnvironment .demo &&
330333 demoDataInitializerService != null ) {
334+ _logger.info (
335+ '[AppBloc] Demo mode: Initializing user-specific data for '
336+ 'user: ${newUser .id }' ,
337+ );
331338 try {
332- _logger.info ('Demo mode: Initializing data for user ${newUser .id }.' );
333339 await demoDataInitializerService! .initializeUserSpecificData (newUser);
334340 _logger.info (
335- 'Demo mode: Data initialization complete for ${newUser .id }.' ,
341+ '[AppBloc] Demo mode: User-specific data initialized for '
342+ 'user: ${newUser .id }.' ,
336343 );
337344 } catch (e, s) {
338- _logger.severe ('ERROR: Failed to initialize demo user data.' , e, s);
339- // If demo data initialization fails, it's a critical error for demo mode.
345+ _logger.severe (
346+ '[AppBloc] ERROR: Failed to initialize demo user data.' ,
347+ e,
348+ s,
349+ );
340350 emit (
341351 state.copyWith (
342352 status: AppLifeCycleStatus .criticalError,
@@ -345,33 +355,55 @@ class AppBloc extends Bloc<AppEvent, AppState> {
345355 ),
346356 ),
347357 );
348- return ; // Stop further processing if demo data init failed critically.
358+ return ; // Stop further processing if initialization failed critically.
349359 }
350360 }
361+
362+ // Handle data migration if an anonymous user signs in.
363+ if (oldUser != null &&
364+ oldUser.appRole == AppUserRole .guestUser &&
365+ newUser.appRole == AppUserRole .standardUser) {
366+ _logger.info (
367+ '[AppBloc] Anonymous user ${oldUser .id } transitioned to '
368+ 'authenticated user ${newUser .id }. Attempting data migration.' ,
369+ );
370+ if (demoDataMigrationService != null &&
371+ _environment == local_config.AppEnvironment .demo) {
372+ try {
373+ await demoDataMigrationService! .migrateAnonymousData (
374+ oldUserId: oldUser.id,
375+ newUserId: newUser.id,
376+ );
377+ _logger.info (
378+ '[AppBloc] Demo mode: Data migration completed for ${newUser .id }.' ,
379+ );
380+ } catch (e, s) {
381+ _logger.severe (
382+ '[AppBloc] ERROR: Failed to migrate demo user data.' ,
383+ e,
384+ s,
385+ );
386+ // If demo data migration fails, it's a critical error for demo mode.
387+ emit (
388+ state.copyWith (
389+ status: AppLifeCycleStatus .criticalError,
390+ initialUserPreferencesError: UnknownException (
391+ 'Failed to migrate demo user data: ${e .toString ()}' ,
392+ ),
393+ ),
394+ );
395+ return ; // Stop further processing if migration failed critically.
396+ }
397+ }
398+ }
399+
400+ // After potential initialization and migration,
401+ // ensure user-specific data (settings and preferences) are loaded.
351402 await _fetchAndSetUserData (newUser, emit);
352403 } else {
353404 // If user logs out, clear user-specific data from state.
354405 emit (state.copyWith (settings: null , userContentPreferences: null ));
355406 }
356-
357- // Handle data migration if an anonymous user signs in.
358- if (oldUser != null &&
359- oldUser.appRole == AppUserRole .guestUser &&
360- newUser != null &&
361- newUser.appRole == AppUserRole .standardUser) {
362- _logger.info (
363- 'Anonymous user ${oldUser .id } transitioned to authenticated user '
364- '${newUser .id }. Attempting data migration.' ,
365- );
366- if (demoDataMigrationService != null &&
367- _environment == local_config.AppEnvironment .demo) {
368- await demoDataMigrationService! .migrateAnonymousData (
369- oldUserId: oldUser.id,
370- newUserId: newUser.id,
371- );
372- _logger.info ('Demo mode: Data migration completed for ${newUser .id }.' );
373- }
374- }
375407 }
376408
377409 /// Handles refreshing/loading app settings (theme, font).
0 commit comments