Skip to content

Poorgramer-Zack/dart-expert-skills / skills/archived/flutter-db

flutter-db

Verified

Local database and persistence selection for Flutter including SharedPreferences, SecureStorage, Hive, and Drift. Use when implementing offline storage, encrypted data persistence, or choosing between key-value and relational local databases.

FileKindSize
SKILL.mdskill md2.7 KB
references/databases-overview.mdmarkdown2.9 KB
references/drift.mdmarkdown6.6 KB
references/hive-ce.mdmarkdown6.2 KB
references/secure-storage.mdmarkdown3.8 KB
references/shared-preferences.mdmarkdown3.1 KB

references/shared-preferences.md

View on GitHub
---
name: "shared-preferences-best-practices"
description: "Best practices for implementing key-value persistence in Flutter using shared_preferences. Includes static wrapper patterns and Riverpod 3.0 reactive integration."
metadata:
  last_modified: "2026-03-12 11:18:17 (GMT+8)"
---

# Shared Preferences Best Practices

## Goal
Provide a simple, persistent storage solution for primitive data types. By encapsulating SharedPreferences into a static wrapper and integrating it with Riverpod 3.0, we ensure easy access throughout the app while maintaining UI reactivity.

## Instructions

### 1. Installation
```yaml
dependencies:
  shared_preferences: ^2.3.5
  flutter_riverpod: ^3.0.0-beta.0 # Or latest 3.x
  riverpod_annotation: ^4.0.0
```

### 2. Static Wrapper Pattern (Encapsulation)
Encapsulating SharedPreferences into a static class allows for clean, synchronous reads and easy maintenance of key constants.

```dart
import 'package:shared_preferences/shared_preferences.dart';

class Prefs {
  static late SharedPreferences _prefs;

  // Key constants
  static const _keyIsDarkMode = 'is_dark_mode';
  static const _keyUserName = 'user_name';

  /// Initialize the instance. Call this in main() before runApp().
  static Future<void> init() async {
    _prefs = await SharedPreferences.getInstance();
  }

  // Getters & Setters
  static bool get isDarkMode => _prefs.getBool(_keyIsDarkMode) ?? false;
  static Future<bool> setDarkMode(bool value) => _prefs.setBool(_keyIsDarkMode, value);

  static String get userName => _prefs.getString(_keyUserName) ?? '';
  static Future<bool> setUserName(String value) => _prefs.setString(_keyUserName, value);
  
  /// Clear all data
  static Future<bool> clear() => _prefs.clear();
}
```

### 3. Application Startup
```dart
void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  
  // Initialize static preferences before app starts
  await Prefs.init();
  
  runApp(const ProviderScope(child: MyApp()));
}
```

### 4. Riverpod 3.0 Reactive Integration
Since SharedPreferences does not natively notify listeners, we wrap the static calls in a Riverpod `Notifier` to provide reactive state to the UI.

```dart
import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'prefs.dart';

part 'settings_provider.g.dart';

@riverpod
class DarkModeState extends _$DarkModeState {
  @override
  bool build() {
    // Read initial value from static wrapper
    return Prefs.isDarkMode;
  }

  Future<void> toggle() async {
    final newValue = !state;
    // 1. Update persistent storage
    await Prefs.setDarkMode(newValue);
    // 2. Update reactive state (UI will rebuild)
    state = newValue;
  }
}
```

## Constraints
* **Static Initialization**: `Prefs.init()` MUST be awaited in `main()` before `runApp`.
* **Encapsulation**: Prohibit direct calls to `SharedPreferences.getInstance()` outside the `Prefs` class to prevent key string fragmentation.
* **Reactivity**: Use Riverpod `Notifier`s to wrap `Prefs` values when the UI needs to automatically rebuild upon change.
* **Primitive Only**: Only store `String`, `int`, `bool`, `double`, and `List<String>`.
deprecation-and-migrationaddyosmani/agent-skillsManages deprecation and migration. Use when removing old systems, APIs, or features. Use when migrating users from one implementation to another. Use when migrating a database schema in production, such as renaming or dropping a column without downtime (expand/contract). Use when deciding whether to maintain or sunset existing code.Stars: 119kUpdated Claude CodeCodexVerifiedInstall
performance-optimizationaddyosmani/agent-skillsOptimizes application performance across frontend, backend, queries, and databases. Use when performance requirements exist, when you suspect performance regressions, when Core Web Vitals or load times need improvement, when N+1 query patterns need fixing, or when profiling reveals bottlenecks.Stars: 119kUpdated Claude CodeCodexVerifiedInstall
agent-cost-reportthedotmack/claude-memBelievable agent cost report for any period, default the last 7 full days PT, not counting today. Measured tokens from Claude Code transcripts priced at OpenRouter list prices (ESTIMATED), measured provider spend when a sanctioned source exists, note-taker cost separate, Timing-style HTML/PDF plus report.json, line-items.csv, evidence.json.Stars: 118kUpdated Claude CodeCodexVerifiedInstall
agent-cost-reportthedotmack/claude-memBelievable agent cost report for any period, default the last 7 full days PT, not counting today. Measured tokens from Claude Code transcripts priced at OpenRouter list prices (ESTIMATED), measured provider spend when a sanctioned source exists, note-taker cost separate, Timing-style HTML/PDF plus report.json, line-items.csv, evidence.json.Stars: 118kUpdated Claude CodeCodexVerifiedInstall