syncache_flutter library
Flutter integration for Syncache - lifecycle management, widgets, and connectivity detection.
This package provides Flutter-specific functionality for Syncache,
including:
- FlutterNetwork: Real connectivity detection using
connectivity_plus - SyncacheScope: Dependency injection and lifecycle management
- CacheBuilder: StreamBuilder-style widget for cache data
- CacheConsumer: Consumer pattern widget with separate listener
- MultiSyncacheScope: Helper for providing multiple cache instances
Getting Started
- Create a FlutterNetwork instance and initialize it:
final network = FlutterNetwork();
await network.initialize();
- Create your cache instances:
final userCache = Syncache<User>(
store: MemoryStore<User>(),
network: network,
);
- Wrap your app with SyncacheScope:
SyncacheScope<User>(
cache: userCache,
network: network,
child: MyApp(),
)
- 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
Networkimplementation that usesconnectivity_plusto 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
Syncacheinstance 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.