OfflineFirstRepository<_RepositoryModel extends OfflineFirstModel> class abstract

A ModelRepository that interacts with a SqliteProvider first before using a Provider from a remote source.

The OfflineFirstRepository should be extended by an implementation in the end class. The implementation can then be accessed via singleton or InheritedWidget. For example:

class MyRepository extends OfflineFirstRepository {
  const MyRepository._(
    RestProvider _restProvider,
    SqliteProvider _sqliteProvider,
  ) : super(
        remoteProvider: _restProvider,
        sqliteProvider: _sqliteProvider,
      );
  factory MyRepository() => _singleton!;

  /// The singleton could be configured on the first call of `MyRepository()` or it can
  /// be set by calling `configure` during app initialization.
  static void configure({
    required RestProvider restProvider,
    required SqliteProvider sqliteProvider,
  }) {
    _singleton = MyRepository._(
      restProvider,
      sqliteProvider,
    );
  }
}
Implementers

Constructors

OfflineFirstRepository({required Provider<Model> remoteProvider, required SqliteProvider sqliteProvider, bool? autoHydrate, MemoryCacheProvider? memoryCacheProvider, required Set<Migration> migrations, String? loggerName})

Properties

autoHydrate bool
Refetch results in the background from remote source when any request is made. Defaults to false.
final
hashCode int
The hash code for this object.
no setterinherited
logger → Logger
User for low-level debugging. The logger name can be defined in the default constructor; it defaults to OfflineFirstRepository.
final
memoryCacheProvider → MemoryCacheProvider
The first data source to speed up otherwise taxing queries. Only caches specified models.
final
migrationManager → MigrationManager
final
remoteProvider → Provider<Model>
The data source that data is pushed to and from.
final
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
sqliteProvider SqliteProvider
The local data source utilized before every operation.
final

Methods

delete<_Model extends _RepositoryModel>(_Model instance, {Query? query}) Future<bool>
Remove a model from SQLite and the remoteProvider
exists<_Model extends _RepositoryModel>({Query? query}) Future<bool>
Check if a _Model is accessible locally. First checks if there's a matching query in memoryCacheProvider and then check sqliteProvider. Does not query remoteProvider.
get<_Model extends _RepositoryModel>({Query? query, bool alwaysHydrate = false, bool hydrateUnexisting = true, bool requireRemote = false, bool seedOnly = false}) Future<List<_Model>>
Load association from SQLite first; if the _Model hasn't been loaded previously, fetch it from remoteProvider and hydrate SQLite. For available query providerArgs see remoteProvider#get SqliteProvider.get.
getAssociation<_Model extends _RepositoryModel>(Query query) Future<List<_Model>?>
Used exclusively by the OfflineFirstAdapter. If there are no results, returns null.
getBatched<_Model extends _RepositoryModel>({Query? query, int batchSize = 50, bool requireRemote = false, bool seedOnly = false}) Future<List<_Model>>
Get all results in series of batchSizes (defaults to 50). Useful for large queries or remote results.
hydrate<_Model extends _RepositoryModel>({bool deserializeSqlite = true, Query? query}) Future<List<_Model>>
Fetch and store results from remoteProvider into SQLite and the memory cache.
initialize() Future<void>
Prepare the environment for future repository functions. It is recommended to call this method within a StatefulWidget's initState to ensure it is only invoked once. It is not automatically invoked.
migrate() Future<void>
Update SQLite structure with only new migrations.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
reset() Future<void>
Destroys all local records - specifically, memoryCache and sqliteProvider's data sources.
storeRemoteResults<_Model extends _RepositoryModel>(List<_Model> models) Future<List<_Model>>
Save response results to SQLite.
toString() String
A string representation of this object.
inherited
upsert<_Model extends _RepositoryModel>(_Model instance, {Query? query}) Future<_Model>
Send a model to remoteProvider and hydrate.

Operators

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