build method

  1. @override
ThemeMode build()
override

Initialize a Notifier.

It is safe to use Ref.watch or Ref.listen inside this method.

If a dependency of this Notifier (when using Ref.watch) changes, then build will be re-executed. On the other hand, the Notifier will not be recreated. Its instance will be preserved between executions of build.

If this method throws, reading this provider will rethrow the error.

Implementation

@override
ThemeMode build() {
  try {
    final savedTheme = JetStorage.read<String>(
      _storageKey,
      defaultValue: ThemeMode.system.name,
    );

    if (savedTheme != null) {
      final themeMode = ThemeMode.values.firstWhere(
        (e) => e.name == savedTheme,
        orElse: () => ThemeMode.system,
      );
      dump('[ThemeSwitcher] Loaded theme: ${themeMode.name}');
      return themeMode;
    }
  } catch (e, stackTrace) {
    dump('[ThemeSwitcher] Error loading theme: $e', stackTrace: stackTrace);
    // Fallback to system theme if loading fails
  }
  return ThemeMode.system;
}