Skip to content
cccskills
FreePublished on GitHub

managing-shared-preferences

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 English summary is being prepared. Showing the original description.

See how to install

Files included (1)

  • SKILL.md4.2 KB

SKILL.md (original)

Review the instructions the agent will receive before you install.

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).

Reviews

No reviews yet. Tell others what you thought.

Comments

No login needed. Share how it worked for you, ask a question, or note where it did not run.

No comments yet. Be the first to write one.

Comments are published right away. Do not include personal information such as email addresses or phone numbers. Up to 2 URLs.

0 / 1,000 characters

This form is protected by reCAPTCHA, and the Google Privacy Policy and Terms of Service apply.

More skills from this repository

Declarative animation patterns using flutter_animate v4.5.x with composable effects (fade, scale, slide, shimmer, shake, blur), GLSL fragment shaders, and scroll-synchronized animations. Use this skill for adding UI animations and transitions, building staggered or sequenced effects, creating custom animation effects, fixing animation jank or performance issues, implementing shimmer loading states, or adding scroll-driven animations. Supports delays, curves, adaptive effects, and easy extension method chaining (.animate()).

The English summary is being prepared. Showing the original description.

Poorgramer-Zack/dart-expert-skills7Updated Aug 11, 2026

Dart 3 modern features (through 3.10) and Effective Dart best practices including records, patterns, sealed classes, dot shorthands, null-aware collection elements, and wildcard variables. Use when adopting Dart 3.7+ syntax, using dot shorthands for enums or static members, using null-aware elements in lists or maps, using wildcard _ parameters, improving code quality, or reviewing Dart code conventions.

The English summary is being prepared. Showing the original description.

Poorgramer-Zack/dart-expert-skills7Updated Aug 11, 2026

Expert-level guidance for Flutter production applications: architecture decisions (Clean/Layered/Hexagonal/Feature-First), advanced state management (BLoC/Riverpod/GetX), performance optimization (eliminating jank, memory profiling, reducing rebuild overhead), code review with design pattern enforcement, scalability assessments, native platform integration (MethodChannel/EventChannel/FFI), complex UI challenges (custom render objects, slivers), testing strategies (unit/widget/integration/golden), CI/CD pipeline architecture, build optimization, and accessibility compliance. Triggers: 'architecture decision', 'design pattern', 'performance issue', 'code review', 'production problem', 'scaling concerns', 'best practices', 'technical debt', 'refactoring strategy'. Essential for complex enterprise apps, critical production debugging, architectural refactors, or when choosing between competing technical approaches.

The English summary is being prepared. Showing the original description.

Poorgramer-Zack/dart-expert-skills7Updated Aug 11, 2026

Builds SEO-friendly web applications with Dart using the Jaspr framework. Use when creating Dart-based websites, blogs, or landing pages requiring semantic HTML output; implementing server-side rendering (SSR) or static site generation (SSG); adding Tailwind CSS via jaspr_tailwind; routing with jaspr_router; testing components with jaspr_test; or building full-stack Dart web apps where Flutter Web's canvas rendering is unsuitable.

The English summary is being prepared. Showing the original description.

Poorgramer-Zack/dart-expert-skills7Updated Aug 11, 2026

shadcn_flutter UI component library for building accessible, themed Flutter interfaces. Use when integrating shadcn components, configuring design system theming, or building consistent widget layouts.

The English summary is being prepared. Showing the original description.

Poorgramer-Zack/dart-expert-skills7Updated Aug 11, 2026

Configures Codemagic CI/CD pipelines for Flutter using `codemagic.yaml`. Use when setting up automated Flutter builds on Codemagic, configuring iOS code signing without Fastlane Match, deploying to App Store Connect or Google Play from Codemagic, caching pub dependencies in Codemagic, setting up encrypted environment variable groups, running tests with Codemagic's test dashboard, or choosing between Codemagic and GitHub Actions + Fastlane for a Flutter CI/CD pipeline.

The English summary is being prepared. Showing the original description.

Poorgramer-Zack/dart-expert-skills7Updated Aug 11, 2026

See all skills by Poorgramer-Zack

Report a problem with this skill