···86868787 @override
8888 SettingsState build() {
8989- // Watch the preferences provider - when it updates, we'll rebuild
9090- ref.watch(userPreferencesProvider);
8989+ // Note: We intentionally don't watch userPreferencesProvider here.
9090+ // Watching it causes rebuilds that can race with loadSettings() and
9191+ // reset the state. Instead, we explicitly call syncPreferencesFromServer()
9292+ // when we need to refresh preferences.
91939294 // Preserve state across rebuilds to prevent feeds tabs from disappearing
9395 listenSelf((previous, next) {
9496 _preservedState = next;
9597 });
96989797- // If we've already loaded settings once, preserve the state instead of
9898- // resetting to default. This prevents the UI from flickering when
9999- // userPreferencesProvider triggers a rebuild.
9999+ // If we've already loaded settings once, preserve the state
100100 if (_hasLoadedSettings && _preservedState != null) {
101101 return _preservedState!;
102102 }
···136136 // Wait for auth to be initialized before trying to load settings
137137 final authRepository = sprkRepository.authRepository;
138138 await authRepository.initializationComplete;
139139+140140+ // Don't load settings if not authenticated - wait for login
141141+ if (!authRepository.isAuthenticated) {
142142+ return;
143143+ }
139144140145 // Get preferences from the provider (waits for it to load if needed)
141146 final preferences = await _getPreferences();
···291296 /// - Manually from the settings UI if user wants to refresh preferences
292297 Future<void> syncPreferencesFromServer() async {
293298 try {
294294- // Reset load state to force a fresh load from server
299299+ // Reset all load state to force a fresh load from server
300300+ // Reset flags to handle race conditions (e.g., login while loading)
295301 _hasLoadedSettings = false;
302302+ _isLoadingSettings = false;
303303+ _defaultLabelerEnsured = false;
304304+ // Refresh preferences from server - use Future to avoid modifying
305305+ // provider during widget build phase
306306+ await Future(() async {
307307+ await ref.read(userPreferencesProvider.notifier).refresh();
308308+ });
296309 await loadSettings();
297310 logger.d('Preferences synced successfully');
298311 } catch (e) {
+5-1
lib/src/sprk_app.dart
···2626 void initState() {
2727 super.initState();
2828 ref.read(themeProvider.notifier).initialize();
2929- _initializeApp();
2929+ // Defer initialization to after the widget tree is built
3030+ // to avoid modifying providers during build
3131+ WidgetsBinding.instance.addPostFrameCallback((_) {
3232+ _initializeApp();
3333+ });
3034 }
31353236 Future<void> _initializeApp() async {