xaldarof / encrypted-shared-preferences

Perfect for developers prioritizing security, Encrypted Shared Preferences integrates effortlessly into your projects, offering peace of mind and enhancing data privacy. Safeguard your app's data with this essential encryption solution
Apache License 2.0
12 stars 2 forks source link

Proposal: Align API with shared_preferences for Hot-Swap Compatibility #9

Closed sionzee closed 1 week ago

sionzee commented 1 month ago

Overview

The encrypted-shared-preferences package offers similar functionality to Flutter's shared_preferences but lacks full API compatibility, making it challenging to swap them seamlessly. Aligning encrypted-shared-preferences with the shared_preferences API would allow developers to use it as a direct replacement without breaking changes.

API Inconsistencies Identified:

  1. Missing Methods:

    • containsKey
    • getAll
    • getKeys
    • getStringList
    • setStringList
  2. Method Signature Differences:

    • clear: Should accept {Set<String>? allowList}
    • getBoolean: Should be renamed to getBool and return a Future
    • getDouble: Should return a Future
    • getInt: Should return a Future
    • getString: Should return a Future
    • setBoolean: Should be renamed to setBool

Proposal

  1. API Compatibility:

    • Implement the missing methods and adapt existing ones to ensure API consistency with shared_preferences.
    • Ensure the methods return the same data types (Future) where applicable.
  2. Adopt SharedPreferencesAsync and SharedPreferencesWithCache Approach:

    • Implement both SharedPreferencesAsync and SharedPreferencesWithCache patterns to provide caching and asynchronous behavior, similar to shared_preferences.
    • Consider deprecating any synchronous methods to maintain consistency with SharedPreferencesAsync.
  3. Default Values:

    • Instead of providing default values, methods should return null, allowing users to handle them with the null-aware operator (??). This approach aligns with modern Dart practices and simplifies integration.
  4. Consistent Asynchronous Behavior:

    • Currently, get methods are synchronous, while set methods are asynchronous. It is preferable for all methods to be asynchronous (Future) for consistency and to handle encrypted operations correctly.

Why This Is Important

Side Note

At the moment, we're forced to write a wrapper around encrypted-shared-preferences to implement SharedPreferencesAsync ourselves. This change would eliminate the need for custom wrappers and simplify secure data management in Flutter applications.

Thank you for considering this request!

xaldarof commented 1 month ago

Hi, thank you for your contribution! I didn’t know about the new shared_preferences APIs. I will work on them soon.

xaldarof commented 1 week ago

10 you can check async version of shared_preferences