API Reference
This section covers the full API reference for QuantumCacheDB, detailing each method and class available for interacting with the database in your Dart or Flutter project.
QuantumCacheDB Class
init()
Signature: Future<void> init();
Initializes the QuantumCacheDB instance, setting up the underlying storage mechanism.
Usage:
var db = QuantumCacheDB();
await db.init();
This method must be called before any other database operations. It sets up the database and prepares it for use.
set(String key, dynamic value)
Signature: Future<void> set(String key, dynamic value);
Stores a value associated with a given key in the database. The value can be any serializable object (like a map, list, or simple data types like strings or numbers).
Usage:
db.set('user_1', {'name': 'John Doe', 'age': 30});
get(String key)
Signature: Future<dynamic> get(String key);
Retrieves the value associated with a given key from the database. If the key does not exist, it returns null.
Usage:
var user = await db.get('user_1');
print(user); // Output: {'name': 'John Doe', 'age': 30}
delete(String key)
Signature: Future<void> delete(String key);
Deletes the record associated with the provided key from the database.
Usage:
await db.delete('user_1');
containsKey(String key)
Signature: Future<bool> containsKey(String key);
Checks if the given key exists in the database. Returns true if the key exists, false otherwise.
Usage:
bool exists = await db.containsKey('user_1');
print(exists); // Output: true or false
watch(String key)
Signature: Stream<dynamic> watch(String key);
Returns a stream that listens for changes to a specific key. Any updates made to the key will emit new values through the stream.
Usage:
Stream userStream = db.watch('user_1');
userStream.listen((userData) {
print('User data updated: $userData');
});
clear()
Signature: Future<void> clear();
Clears the entire database by removing all stored keys and values.
Usage:
await db.clear();
This method is useful when you want to completely reset the database.
Data Types Supported
QuantumCacheDB supports various data types for storage, including:
- String
- Number (int, double)
- Map
- List
- Boolean
You can store any of these types as long as they are serializable.
Reactive Database Model
For real-time updates, QuantumCacheDB supports a reactive model using streams. The watch method allows you to listen to changes on specific keys. Whenever data associated with the key is updated, the stream emits the new data, allowing your app to respond to changes instantly.
Example: Listening for Data Changes
Stream<Map<String, dynamic>> userStream = db.watch('user_1');
userStream.listen((updatedUser) {
print('Updated user: $updatedUser');
});
Error Handling
- Database Initialization Errors: If the database fails to initialize, an exception will be thrown. Make sure to handle these errors using
try-catch. - Key Not Found Errors: If you try to access a key that does not exist, the
getmethod will returnnull. You can check for existence usingcontainsKeybefore accessing a key.