@@ -86,8 +86,32 @@ class AccountBloc extends Bloc<AccountEvent, AccountState> {
8686 if (_environment == local_config.AppEnvironment .demo) {
8787 // ignore: inference_failure_on_instance_creation
8888 await Future .delayed (const Duration (seconds: 1 ));
89+ // After delay, re-attempt to read the preferences.
90+ // This is crucial because migration might have completed during the delay.
91+ try {
92+ final migratedPreferences =
93+ await _userContentPreferencesRepository.read (
94+ id: event.userId,
95+ userId: event.userId,
96+ );
97+ emit (
98+ state.copyWith (
99+ status: AccountStatus .success,
100+ preferences: migratedPreferences,
101+ clearErrorMessage: true ,
102+ ),
103+ );
104+ return ; // Exit if successfully read after migration
105+ } on NotFoundException {
106+ // Still not found after delay, proceed to create default.
107+ print (
108+ '[AccountBloc] UserContentPreferences still not found after '
109+ 'migration delay. Creating default preferences.' ,
110+ );
111+ }
89112 }
90- // If preferences not found, create a default one for the user
113+ // If preferences not found (either initially or after re-attempt),
114+ // create a default one for the user.
91115 final defaultPreferences = UserContentPreferences (id: event.userId);
92116 try {
93117 await _userContentPreferencesRepository.create (
@@ -101,11 +125,32 @@ class AccountBloc extends Bloc<AccountEvent, AccountState> {
101125 clearErrorMessage: true ,
102126 ),
103127 );
128+ } on ConflictException {
129+ // If a conflict occurs during creation (e.g., another process
130+ // created it concurrently), attempt to read it again to get the
131+ // existing one. This can happen if the migration service
132+ // created it right after the second NotFoundException.
133+ print (
134+ '[AccountBloc] Conflict during creation of UserContentPreferences. '
135+ 'Attempting to re-read.' ,
136+ );
137+ final existingPreferences =
138+ await _userContentPreferencesRepository.read (
139+ id: event.userId,
140+ userId: event.userId,
141+ );
142+ emit (
143+ state.copyWith (
144+ status: AccountStatus .success,
145+ preferences: existingPreferences,
146+ clearErrorMessage: true ,
147+ ),
148+ );
104149 } catch (e) {
105150 emit (
106151 state.copyWith (
107152 status: AccountStatus .failure,
108- errorMessage: 'Failed to create default preferences. ' ,
153+ errorMessage: 'Failed to create default preferences: $ e ' ,
109154 ),
110155 );
111156 }
0 commit comments