agentleFS
Poorgramer-ZackPoorgramer-Zack/dart-expert-skills/skills/flutter-shared-preferences/SKILL.md

managing-shared-preferences

Skill7 starsChanged 8 months agoMITView on GitHub

SharedPreferences (v2.5.5) for Flutter providing simple persistent key-value storage for non-sensitive user settings and app preferences. Use this skill when storing user preferences (theme mode, language selection, font size), app settings and configuration, feature flags or A/B test assignments, simple cache data, onboarding completion status, last selected values (filters, sort options), user consent flags, or any non-sensitive data that survives app restarts. Supports String, int, double, bool, and StringList types. NOT suitable for storing tokens, passwords, API keys, or any sensitive/encrypted data (use flutter_secure_storage instead). Ideal for quick persistent storage without database overhead.

The file

We split this skill into the rules it gives an agent

5 rules. About 0 are common, 5 are distinctive, and 0 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈5

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 0

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Create a type-safe static wrapper to prevent key fragmentation:

    line 24
  2. Distinctive:

    No Sensitive Data: NEVER store passwords, tokens, API keys, or PII.

    line 124
  3. Distinctive:

    Type Safety: Always use static wrappers to prevent string key typos.

    line 125
  4. Distinctive:

    Size Limit: Keep stored data small. Not suitable for large datasets or complex objects.

    line 126
  5. Distinctive:

    Synchronous After Init: After initialization, getters are synchronous.

    line 127

≈ “Common” is an estimate: at least 1 in 5 of the 213,897 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

SharedPreferences Simple Storage Guide

Goal

Implement lightweight key-value storage for non-sensitive app settings and user preferences using SharedPreferences. Best for simple configuration data that persists across app sessions.

Process

Phase 1: Install Dependencies

dependencies:
  shared_preferences: ^2.5.5

Phase 2: Create Static Wrapper (Best Practice)

Create a type-safe static wrapper to prevent key fragmentation:

import 'package:shared_preferences/shared_preferences.dart';

class Prefs {
  static late SharedPreferences _instance;
  
  static Future<void> init() async {
    _instance = await SharedPreferences.getInstance();
  }
  
  // Theme preference
  static bool get isDarkMode => _instance.getBool(_Keys.isDarkMode) ?? false;
  static Future<void> setDarkMode(bool value) => _instance.setBool(_Keys.isDarkMode, value);
  
  // User name
  static String? get userName => _instance.getString(_Keys.userName);
  static Future<void> setUserName(String value) => _instance.setString(_Keys.userName, value);
  
  // Onboarding completed
  static bool get hasCompletedOnboarding => _instance.getBool(_Keys.onboardingCompleted) ?? false;
  static Future<void> setOnboardingCompleted(bool value) => _instance.setBool(_Keys.onboardingCompleted, value);
  
  // Feature flags
  static bool get isFeatureXEnabled => _instance.getBool(_Keys.featureX) ?? false;
  static Future<void> setFeatureX(bool value) => _instance.setBool(_Keys.featureX, value);
  
  // Clear all preferences
  static Future<void> clear() => _instance.clear();
}

class _Keys {
  static const String isDarkMode = 'is_dark_mode';
  static const String userName = 'user_name';
  static const String onboardingCompleted = 'onboarding_completed';
  static const String featureX = 'feature_x_enabled';
}

Phase 3: Initialize in main()

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Prefs.init();
  runApp(MyApp());
}

Phase 4: Usage in App

// Read
final isDark = Prefs.isDarkMode;
final name = Prefs.userName ?? 'Guest';

// Write
await Prefs.setDarkMode(true);
await Prefs.setUserName('John Doe');

// Clear all
await Prefs.clear();

Phase 5: Integration with State Management

With Riverpod:

final darkModeProvider = StateNotifierProvider<DarkModeNotifier, bool>((ref) {
  return DarkModeNotifier();
});

class DarkModeNotifier extends StateNotifier<bool> {
  DarkModeNotifier() : super(Prefs.isDarkMode);
  
  Future<void> toggle() async {
    state = !state;
    await Prefs.setDarkMode(state);
  }
}

With BLoC:

class ThemeCubit extends Cubit<ThemeState> {
  ThemeCubit() : super(ThemeState(isDark: Prefs.isDarkMode));
  
  Future<void> toggleTheme() async {
    final newValue = !state.isDark;
    await Prefs.setDarkMode(newValue);
    emit(state.copyWith(isDark: newValue));
  }
}

Constraints

  • No Sensitive Data: NEVER store passwords, tokens, API keys, or PII. Use SecureStorage instead.
  • Type Safety: Always use static wrappers to prevent string key typos.
  • Size Limit: Keep stored data small. Not suitable for large datasets or complex objects.
  • Synchronous After Init: After initialization, getters are synchronous. Only setters are async.
  • Platform Limits: iOS has NSUserDefaults limits (~1MB recommended).

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.