SerializableStorageContainer<E extends Object> class
abstract
An abstract class that provides a container for storing serializable objects
of type E.
This class extends StorageContainer and implements
SerializableStorageOperationsApi, providing a comprehensive set of CRUD
operations for storing, retrieving, and managing serializable objects. Each
object is associated with a unique ID, which can either be extracted from
the object using an IdGetter or automatically generated.
Key Features
- Object Storage: Store and retrieve typed objects, not just primitives
- Automatic Serialization: Objects are automatically serialized to strings for storage
- ID Management: Supports both custom ID extraction and automatic ID generation
- Batch Operations: Efficiently handle multiple objects at once with
setAll,addAll, etc. - Type Safety: Generic type parameter ensures type-safe operations
- Full CRUD Support: Complete create, read, update, delete functionality
Usage
To use this class, you must:
- Extend it and provide a type parameter
- Implement serialize to convert objects to strings
- Implement deserialize to convert strings back to objects
ID Management
The container supports two modes for managing object IDs:
Custom ID Extraction
Provide an IdGetter function to extract IDs from objects:
SerializableStorageContainer<User>(
backend: backend,
name: 'users',
idGetter: (user) => user.id, // Extract ID from object
);
Automatic ID Generation
If no IdGetter is provided, IDs are automatically generated:
SerializableStorageContainer<Note>(
backend: backend,
name: 'notes',
// No idGetter - IDs will be auto-generated
);
Subclasses must implement:
- serialize: Convert an object of type
Eto a String - deserialize: Convert a String back to an object of type
E
See also:
StorageContainerfor the base container functionality- SerializableStorageOperationsApi for the operation interface
- IdGetter for custom ID extraction
- Inheritance
-
- Object
- BaseStorage
- SerializableStorageContainer
- Implemented types
- Implementers
Constructors
-
SerializableStorageContainer({IdGetter<
E> ? idGetter, required StorageBackend backend, required String name, String? delimiter, Random? random, int? seed}) - Creates a new SerializableStorageContainer.
Properties
- delimiter → String
-
The character(s) used to separate the container name from the key.
finalinherited
- hashCode → int
-
The hash code for this object.
no setterinherited
- hasListeners → bool
-
Returns
trueif there are any listeners registered (global or keyed).no setterinherited -
idGetter
→ IdGetter<
E> ? -
An optional function that extracts the ID from an object.
final
-
isEmpty
→ Future<
bool> -
Returns
trueif the container has no stored objects.no setteroverride -
isNotEmpty
→ Future<
bool> -
Returns
trueif the container has at least one stored object.no setteroverride - name → String
-
The name of the storage container, used for namespacing keys.
finalinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
add(
E value) → Future< void> -
Adds an object to the container using its ID.
override
-
addAll(
Iterable< E> values) → Future<void> -
Adds multiple objects to the container using their IDs.
override
-
addKeyListener(
String key, ListenableCallback listener) → void -
Registers a listener to be called when data for a specific
keychanges.inherited -
addListener(
ListenableCallback listener) → void -
Registers a global listener to be called when any data in the storage
changes.
inherited
-
clear(
) → Future< void> - Removes all objects from the container.
-
close(
) → Future< void> - Closes the container and releases all resources.
-
containsKey(
String key) → Future< bool> -
Checks if an object with the given key exists in the container.
override
-
deserialize(
String value) → E -
Deserializes a string back to an object of type
E.override -
generateId(
) → String - Generates a new unique ID for an object.
-
get(
String? key) → Future< E?> -
Retrieves an object by its key.
override
-
getAll(
[Iterable< String> ? allowList]) → Future<Map< String, E> > -
Retrieves multiple objects from the container.
override
-
getDecodedKeys(
) → Future< Set< String> > -
Returns a set of all decoded keys in the container.
inherited
-
getEncodedKeys(
) → Future< Set< String> > -
Returns a set of all encoded keys in the container.
inherited
-
getId(
E value) → String - Gets the ID for the given object.
-
getKeys(
) → Future< Set< String> > -
Returns all keys (IDs) of objects stored in the container.
override
-
getValues(
) → Future< List< E> > -
Retrieves all objects from the container as a list.
override
-
hasKeyListeners(
String key) → bool -
Returns
trueif there are any listeners registered for the givenkey.inherited -
itemHolder(
String key) → SerializableItemHolder< E> - Creates an item holder for storing a single primitive value at the specified key.
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
notifyKeyListeners(
String key) → void -
Notifies all listeners that are registered for a specific
key.inherited -
notifyListeners(
[String? key]) → void -
Notifies all registered listeners of a change.
inherited
-
remove(
String key) → Future< void> -
Removes an object by its key.
override
-
removeAll(
Iterable< String> keys) → Future<void> -
Removes multiple objects by their keys.
override
-
removeAllItems(
Iterable< E> items) → Future<void> -
Removes multiple objects from the container.
override
-
removeAllKeyListeners(
String key) → void -
Removes all listeners registered for a specific
key.inherited -
removeAllListeners(
) → void -
Removes all registered listeners, both global and key-specific.
inherited
-
removeItem(
E item) → Future< void> -
Removes an object from the container.
override
-
removeKeyListener(
String key, ListenableCallback listener) → void -
Removes a previously registered key-specific listener.
inherited
-
removeListener(
ListenableCallback listener) → void -
Removes a previously registered global listener.
inherited
-
serialize(
E value) → String -
Serializes an object of type
Eto a string for storage.override -
set(
String key, E value) → Future< void> -
Stores an object with the specified key.
override
-
setAll(
Map< String, E> items) → Future<void> -
Stores multiple objects with their associated keys.
override
-
stream(
String key) → Stream< E?> -
Provides a Stream of values for the given
key. The stream will emit the current value of the key and will update whenever the value changes. -
streamAll(
) → Stream< List< E> > - Provides a Stream of values for all items in the container. The stream will emit the current values and will update whenever any value changes.
-
toString(
) → String -
A string representation of this object.
inherited
-
update(
E value) → Future< void> -
Updates an existing object in the container.
override
-
updateAll(
Iterable< E> values) → Future<void> -
Updates multiple existing objects in the container.
override
-
validateKeys(
Iterable< String> ? keys) → void -
Validates a collection of storage
keysto ensure they all meet requirements.inherited -
validateName(
String name) → void -
Validates the given container
nameto ensure it meets requirements.inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited