Skip to content

Commit e22610c

Browse files
authored
Merge pull request #173 from flutter-news-app-full-source-code/fix/unmigrated-demo-account-data
fix(app): correct data migration logic in demo mode
2 parents 8dd8d04 + c658a1e commit e22610c

File tree

2 files changed

+16
-12
lines changed

2 files changed

+16
-12
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## 1.4.0 - Upcoming Release
44

5+
- **fix(demo)**: correct data migration logic for anonymous to authenticated user transitions
56
- **refactor**: improved filter page reset button to clear local selections and disable when not applicable
67
- **feat**: auto-scroll to active filter chip in SavedFiltersBar
78
- **fix**: resolve bug where applying a modified filter incorrectly selects the original saved filter

lib/app/bloc/app_bloc.dart

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -336,11 +336,15 @@ class AppBloc extends Bloc<AppEvent, AppState> {
336336
// If a new user is present, handle their data.
337337
if (newUser != null) {
338338
// In demo mode, ensure essential user-specific data (settings,
339-
// preferences, and the user object itself in the data client)
340-
// are initialized if they don't already exist. This prevents
341-
// NotFoundException during subsequent reads.
342-
if (_environment == local_config.AppEnvironment.demo &&
343-
demoDataInitializerService != null) {
339+
// preferences) are initialized if they don't already exist.
340+
final isDemoMode = _environment == local_config.AppEnvironment.demo;
341+
final isMigration =
342+
oldUser != null &&
343+
oldUser.appRole == AppUserRole.guestUser &&
344+
newUser.appRole == AppUserRole.standardUser;
345+
346+
// Initialize data for a new user, but ONLY if it's not a migration.
347+
if (isDemoMode && !isMigration && demoDataInitializerService != null) {
344348
_logger.info(
345349
'[AppBloc] Demo mode: Initializing user-specific data for '
346350
'user: ${newUser.id}',
@@ -376,9 +380,7 @@ class AppBloc extends Bloc<AppEvent, AppState> {
376380
}
377381

378382
// Handle data migration if an anonymous user signs in.
379-
if (oldUser != null &&
380-
oldUser.appRole == AppUserRole.guestUser &&
381-
newUser.appRole == AppUserRole.standardUser) {
383+
if (isMigration) {
382384
_logger.info(
383385
'[AppBloc] Anonymous user ${oldUser.id} transitioned to '
384386
'authenticated user ${newUser.id}. Attempting data migration.',
@@ -414,10 +416,11 @@ class AppBloc extends Bloc<AppEvent, AppState> {
414416
}
415417
}
416418

417-
// If not in demo mode, or if the demo initializer is not used,
418-
// perform the standard data fetch. In demo mode, this call is now
419-
// redundant as it's handled immediately after initialization.
420-
if (_environment != local_config.AppEnvironment.demo) {
419+
// Perform the standard data fetch if we are not in demo mode, or if a
420+
// migration just occurred (to load the migrated data).
421+
// This avoids a redundant fetch in the demo initialization path, which
422+
// handles its own data fetching after creating fixture data.
423+
if (!isDemoMode || isMigration) {
421424
await _fetchAndSetUserData(newUser, emit);
422425
}
423426
} else {

0 commit comments

Comments
 (0)