StorageService class
Shared local storage service (wraps SharedPreferences).
Registered once in main: Get.put(StorageService(prefs), permanent: true).
_prefs is null when SharedPreferences.getInstance() fails at boot
(rare devices, corrupted storage) — the in-memory _fallback is used so
the app can still run with safe default values instead of a hard crash
(it doesn't persist across sessions, but that's a tradeoff to fix later,
not a crash).
- Inheritance
-
- Object
- GetLifeCycle
- DisposableInterface
- GetxService
- StorageService
Constructors
- StorageService(SharedPreferences? _prefs)
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- initialized → bool
-
Checks whether the controller has already been initialized.
no setterinherited
- isClosed → bool
-
Checks whether the controller has already been closed.
no setterinherited
-
onDelete
→ InternalFinalCallback<
void> -
Internal callback that starts the cycle of this controller.
finalinherited
-
onStart
→ InternalFinalCallback<
void> -
Called at the exact moment the widget is allocated in memory.
It uses an internal "callable" type, to avoid any @overrides in subclases.
This method should be internal and is required to define the
lifetime cycle of the subclass.
finalinherited
- platformWrites ↔ int
-
Count of actual
SharedPreferenceshits (platform channel + disk write). Incremented on everysetXthat does not go through the buffer, and on every key flush pushes down.getter/setter pair - runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
$configureLifeCycle(
) → void -
inherited
-
allKeys(
) → Set< String> -
Every key currently in storage. Same reasoning as exportAll — uses
the generic API instead of hand-listing
StorageKeys, so there's no place that must remember to update a hand-written list every time a new key is added (that kind of hand-written list drifts out of date very easily across a few rounds of code review). -
eraseAll(
) → Future< void> -
Wipes every key currently in storage — the classic "Reset progress"
Settings action. A thin, self-explanatory name over what
importAll(const {})already did silently: importAll REPLACES the whole profile (see_replaceAll), so an empty map means "keep nothing" — but a consumer app had to know that trick rather than see an API that says what it does. Same rollback-on-error guarantee as importAll: a failure partway through leaves storage exactly as it was before this call, never half-erased. -
exportAll(
) → Map< String, Object> - Dumps all current storage into a map — uses SharedPreferences's generic API (getKeys/get) instead of hand-listing every StorageKeys entry, so this list doesn't need updating every time a new key is added.
-
exportWithPrefix(
String prefix) → Map< String, Object> -
Every key currently in storage that starts with
prefix— e.g. backing up/cloud-syncing just one save slot's namespaced keys (IDEA-56) without capturing any other slot's.prefixmust not be empty — same reasoning as removeAllWithPrefix: an empty prefix would just be exportAll under a name that doesn't say so. -
flush(
) → Future< void> - Pushes every buffered value down to disk. Call this at safe checkpoints: level end, app going to background, controller disposal, and before any transaction that grants a reward.
-
getBool(
String key, {bool def = false}) → bool -
getDouble(
String key, {double def = 0.0}) → double -
getInt(
String key, {int def = 0}) → int -
getString(
String key) → String? -
getStringList(
String key) → List< String> - Reads a JSON list (unlike the CSV-join pattern of other keys in this file) — used for keys that need to store a list of strings instead of a single value.
-
importAll(
Map< String, Object?> data) → Future<void> - Overwrites storage from an exported map. Values can only be int/bool/double/String (StorageService never sets a List directly on SharedPreferences — setStringList JSON-encodes it to a String itself).
-
importWithPrefix(
String prefix, Map< String, Object?> data) → Future<void> -
Restores
dataas the complete set of keys underprefix, leaving every OTHER key untouched — the write-side counterpart of exportWithPrefix. A key already underprefixbut missing fromdatais removed (mirrors importAll REPLACING a profile, scoped to just this prefix). Every key indatamust start withprefix— guards against a caller accidentally restoring another slot's backup into this one. -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
onClose(
) → void -
Called before
onDeletemethod.onClosemight be used to dispose resources used by the controller. Like closing events, or streams before the controller is destroyed. Or dispose objects that can potentially create some memory leaks, like TextEditingControllers, AnimationControllers. Might be useful as well to persist some data on disk.inherited -
onInit(
) → void -
Called immediately after the widget is allocated in memory.
You might use this to initialize something for the controller.
inherited
-
onReady(
) → void -
Called 1 frame after onInit(). It is the perfect place to enter
navigation events, like snackbar, dialogs, or a new route, or
async request.
inherited
-
remove(
String key) → Future< void> -
removeAllWithPrefix(
String prefix) → Future< void> -
Removes every key currently in storage that starts with
prefix— e.g. deleting one save slot's namespaced keys (IDEA-56) without touching any other slot's. -
setBool(
String key, bool value) → Future< void> - See the note on setInt.
-
setDouble(
String key, double value) → Future< void> - See the note on setInt.
-
setInt(
String key, int value) → Future< void> -
A direct write must invalidate the buffered copy of the same key.
Without this line, the stale buffered value would still shadow the
result in
_raw, and the next flush would overwrite it right back down to disk — an undo, reset, purchase, or import would silently have no effect if that key had ever gone through the hot path. -
setIntBuffered(
String key, int value) → Future< void> -
setString(
String key, String value) → Future< void> - See the note on setInt.
-
setStringBuffered(
String key, String value) → Future< void> -
setStringList(
String key, List< String> value) → Future<void> -
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited
Static Properties
- maybe → StorageService?
-
Safe to call from a standalone widget test that hasn't registered the
service (e.g. small effect widgets like confetti/mascot/pulse-glow
tested in isolation).
no setter
- to → StorageService
-
no setter