@@ -143,12 +143,17 @@ class AppBloc extends Bloc<AppEvent, AppState> {
143143 '[AppBloc] Failed to fetch user settings (HttpException) '
144144 'for user ${user .id }: ${e .runtimeType } - ${e .message }' ,
145145 );
146- emit (
147- state.copyWith (
148- status: AppLifeCycleStatus .criticalError,
149- initialUserPreferencesError: e,
150- ),
151- );
146+ // In demo mode, NotFoundException for user settings is expected if not yet initialized.
147+ // Do not transition to criticalError immediately.
148+ if (_environment != local_config.AppEnvironment .demo ||
149+ e is ! NotFoundException ) {
150+ emit (
151+ state.copyWith (
152+ status: AppLifeCycleStatus .criticalError,
153+ initialUserPreferencesError: e,
154+ ),
155+ );
156+ }
152157 } catch (e, s) {
153158 _logger.severe (
154159 '[AppBloc] Unexpected error during user settings fetch '
@@ -185,12 +190,17 @@ class AppBloc extends Bloc<AppEvent, AppState> {
185190 '[AppBloc] Failed to fetch user content preferences (HttpException) '
186191 'for user ${user .id }: ${e .runtimeType } - ${e .message }' ,
187192 );
188- emit (
189- state.copyWith (
190- status: AppLifeCycleStatus .criticalError,
191- initialUserPreferencesError: e,
192- ),
193- );
193+ // In demo mode, NotFoundException for user content preferences is expected if not yet initialized.
194+ // Do not transition to criticalError immediately.
195+ if (_environment != local_config.AppEnvironment .demo ||
196+ e is ! NotFoundException ) {
197+ emit (
198+ state.copyWith (
199+ status: AppLifeCycleStatus .criticalError,
200+ initialUserPreferencesError: e,
201+ ),
202+ );
203+ }
194204 } catch (e, s) {
195205 _logger.severe (
196206 '[AppBloc] Unexpected error during user content preferences fetch '
@@ -313,29 +323,37 @@ class AppBloc extends Bloc<AppEvent, AppState> {
313323
314324 emit (state.copyWith (status: newStatus));
315325
316- // If a new user is present, fetch their specific data.
326+ // If a new user is present, handle their data.
317327 if (newUser != null ) {
328+ // In demo mode, ensure user-specific data is initialized BEFORE fetching.
329+ if (_environment == local_config.AppEnvironment .demo &&
330+ demoDataInitializerService != null ) {
331+ try {
332+ _logger.info ('Demo mode: Initializing data for user ${newUser .id }.' );
333+ await demoDataInitializerService! .initializeUserSpecificData (newUser);
334+ _logger.info (
335+ 'Demo mode: Data initialization complete for ${newUser .id }.' ,
336+ );
337+ } 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.
340+ emit (
341+ state.copyWith (
342+ status: AppLifeCycleStatus .criticalError,
343+ initialUserPreferencesError: UnknownException (
344+ 'Failed to initialize demo user data: ${e .toString ()}' ,
345+ ),
346+ ),
347+ );
348+ return ; // Stop further processing if demo data init failed critically.
349+ }
350+ }
318351 await _fetchAndSetUserData (newUser, emit);
319352 } else {
320353 // If user logs out, clear user-specific data from state.
321354 emit (state.copyWith (settings: null , userContentPreferences: null ));
322355 }
323356
324- // In demo mode, ensure user-specific data is initialized.
325- if (_environment == local_config.AppEnvironment .demo &&
326- demoDataInitializerService != null &&
327- newUser != null ) {
328- try {
329- _logger.info ('Demo mode: Initializing data for user ${newUser .id }.' );
330- await demoDataInitializerService! .initializeUserSpecificData (newUser);
331- _logger.info (
332- 'Demo mode: Data initialization complete for ${newUser .id }.' ,
333- );
334- } catch (e, s) {
335- _logger.severe ('ERROR: Failed to initialize demo user data.' , e, s);
336- }
337- }
338-
339357 // Handle data migration if an anonymous user signs in.
340358 if (oldUser != null &&
341359 oldUser.appRole == AppUserRole .guestUser &&
@@ -406,6 +424,7 @@ class AppBloc extends Bloc<AppEvent, AppState> {
406424
407425 final updatedSettings = event.settings;
408426
427+ // Optimistically update the state.
409428 emit (state.copyWith (settings: updatedSettings));
410429
411430 try {
0 commit comments