-
Notifications
You must be signed in to change notification settings - Fork 0
Open
Description
Hi @SPiercer ππ»
Thank you for creating this excellent package! I think this enhancement would make it even more convenient to use ππ»
π€ Problem
Currently, users need to manually implement the Storage interface for each backend they want to use. The README shows examples for:
- SharedPreferences
- HydratedBloc
- Hive
While these examples are helpful, every user has to copy-paste and maintain this boilerplate code in their own projects.
// Users must implement this for each storage backend
class SharedPreferencesStorage implements Storage {
final SharedPreferences _prefs;
SharedPreferencesStorage(this._prefs);
@override
dynamic read(String key) {
final value = _prefs.getString(key);
if (value == null) return null;
return jsonDecode(value);
}
@override
Future<void> write(String key, dynamic value) async {
await _prefs.setString(key, jsonEncode(value));
}
// ... more boilerplate
}π‘ Proposed Solution
Provide ready-to-use storage adapters either:
Option 1: Built-in Adapters π§
Include common adapters directly in the saveable package:
import 'package:saveable/saveable.dart';
import 'package:saveable/adapters.dart'; // Built-in adapters
void main() async {
final prefs = await SharedPreferences.getInstance();
final storage = SharedPreferencesStorageAdapter(prefs); // β¨ Ready to use
Saveable.setDefaultStorage(storage);
}Option 2: Separate Adapter Packages π¦ (Recommended)
Create separate packages for each storage backend:
Core package:
saveable- Core functionality +Storageinterface
Adapter packages:
saveable_shared_preferences- SharedPreferences adaptersaveable_hive- Hive adaptersaveable_hydrated_bloc- HydratedBloc adapter- etc..
// Clean and simple usage
import 'package:saveable/saveable.dart';
import 'package:saveable_shared_preferences/saveable_shared_preferences.dart';
void main() async {
final prefs = await SharedPreferences.getInstance();
final storage = SaveableSharedPreferences(prefs); // β¨ From adapter package
Saveable.setDefaultStorage(storage);
}You can check dio_cache_interceptor package for more details is it is following this pattern.
π― Benefits
- Less boilerplate π: No need to copy-paste adapter code
- Tested & maintained β : Official adapters are tested and kept up-to-date
- Smaller bundle size π¦: With separate packages, users only include what they need
- Community contributions π€: Easier for community to contribute new adapters
- Follows best practices π: Matches patterns from popular packages like
dio_cache_interceptor
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels