ZenStorage class abstract

Interface for persisting query data.

Implement this to enable offline support and hydration for ZenQuery.

Important: treat json as an opaque Map<String, dynamic>

Both ZenQueryCache ({data, timestamp, version}) and ZenMutationQueue ({queue, deadLetter} under key 'zen_mutation_queue') persist through ZenStorage. Always serialize/deserialize the entire map rather than assuming specific top-level keys, and preserve nested Map<String, dynamic> types on read (e.g. via jsonEncode / jsonDecode).

Copy-paste recipes

See doc/offline_guide.md for full production recipes covering SharedPreferencesAsync, Drift, Hive CE, and FlutterSecureStorage.

SharedPreferencesAsync

import 'dart:convert';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:zenify/zenify.dart';

class SharedPreferencesStorage implements ZenStorage {
  final String prefix;
  final SharedPreferencesAsync _prefs;
  SharedPreferencesStorage({
    this.prefix = 'zen_query_',
    SharedPreferencesAsync? prefs,
  }) : _prefs = prefs ?? SharedPreferencesAsync();

  @override
  Future<void> write(String key, Map<String, dynamic> json) =>
      _prefs.setString('$prefix$key', jsonEncode(json));

  @override
  Future<Map<String, dynamic>?> read(String key) async {
    final raw = await _prefs.getString('$prefix$key');
    if (raw == null) return null;
    final decoded = jsonDecode(raw);
    return decoded is Map<String, dynamic> ? decoded : null;
  }

  @override
  Future<void> delete(String key) => _prefs.remove('$prefix$key');
}

SQLite / Drift (opaque JSON payload — safe for queries & mutation queue)

import 'dart:convert';
import 'package:drift/drift.dart';
import 'package:zenify/zenify.dart';

class DriftZenStorage implements ZenStorage {
  final AppDatabase _db;
  DriftZenStorage(this._db);

  @override
  Future<void> write(String key, Map<String, dynamic> json) =>
      _db.into(_db.zenCacheEntries).insertOnConflictUpdate(
        ZenCacheEntriesCompanion.insert(
          cacheKey: key,
          payload: jsonEncode(json),
          updatedAt: DateTime.now().millisecondsSinceEpoch,
        ),
      );

  @override
  Future<Map<String, dynamic>?> read(String key) async {
    final row = await (_db.select(_db.zenCacheEntries)
          ..where((t) => t.cacheKey.equals(key)))
        .getSingleOrNull();
    if (row == null) return null;
    final decoded = jsonDecode(row.payload);
    return decoded is Map<String, dynamic> ? decoded : null;
  }

  @override
  Future<void> delete(String key) =>
      (_db.delete(_db.zenCacheEntries)
        ..where((t) => t.cacheKey.equals(key))).go();
}
Implementers

Constructors

ZenStorage()

Properties

hashCode → int
The hash code for this object.
no setterinherited
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited

Methods

delete(String key) → Future<void>
Delete data from storage.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
read(String key) → Future<Map<String, dynamic>?>
Read data from storage.
toString() → String
A string representation of this object.
inherited
write(String key, Map<String, dynamic> json) → Future<void>
Write data to storage.

Operators

operator ==(Object other) → bool
The equality operator.
inherited