syncache_flutter library

Flutter integration for Syncache - lifecycle management, widgets, and connectivity detection.

This package provides Flutter-specific functionality for Syncache, including:

Getting Started

  1. Create a FlutterNetwork instance and initialize it:
final network = FlutterNetwork();
await network.initialize();
  1. Create your cache instances:
final userCache = Syncache<User>(
  store: MemoryStore<User>(),
  network: network,
);
  1. Wrap your app with SyncacheScope:
SyncacheScope<User>(
  cache: userCache,
  network: network,
  child: MyApp(),
)
  1. Use CacheBuilder to display cache data:
CacheBuilder<User>(
  cacheKey: 'user:123',
  fetch: (req) => api.getUser(123),
  builder: (context, snapshot) {
    if (!snapshot.hasData) {
      return const CircularProgressIndicator();
    }
    return UserCard(user: snapshot.data!);
  },
)

Multiple Cache Types

For apps with multiple cache types, use MultiSyncacheScope:

MultiSyncacheScope(
  network: network,
  configs: [
    SyncacheScopeConfig<User>(userCache),
    SyncacheScopeConfig<Post>(postCache),
  ],
  child: MyApp(),
)

Classes

CacheBuilder<T>
A widget that builds itself based on the latest cache value.
CacheConsumer<T>
A widget that listens to cache changes and optionally rebuilds.
FlutterNetwork
A Network implementation that uses connectivity_plus to detect actual network connectivity status.
LifecycleConfig
Configuration for lifecycle-based cache behavior.
MultiSyncacheScope
Convenience widget to provide multiple caches without deep nesting.
SyncacheLifecycleObserver<T>
Observes app lifecycle and triggers cache operations accordingly.
SyncacheScope<T>
Provides a Syncache instance to descendant widgets and manages its lifecycle integration with Flutter.
SyncacheScopeConfig<T>
Type-safe configuration for a single cache scope.
SyncacheValueListenable<T>
A ValueListenable wrapper for Syncache streams.
WatcherRegistration<T>
Tracks an active watcher for lifecycle-based refetching.

Extensions

SyncacheFlutterExtensions on Syncache<T>
Flutter-specific extensions for Syncache.