|
| 1 | +# syncache_flutter |
| 2 | + |
| 3 | +Flutter integration for [Syncache](https://pub.dev/packages/syncache) - lifecycle management, widgets, and connectivity detection. |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +```yaml |
| 8 | +dependencies: |
| 9 | + syncache: ^0.1.0 |
| 10 | + syncache_flutter: ^0.1.0 |
| 11 | +``` |
| 12 | +
|
| 13 | +## Quick Start |
| 14 | +
|
| 15 | +### 1. Provide the cache with SyncacheScope |
| 16 | +
|
| 17 | +Wrap your app (or a subtree) with `SyncacheScope` to provide cache instances to descendants: |
| 18 | + |
| 19 | +```dart |
| 20 | +import 'package:syncache/syncache.dart'; |
| 21 | +import 'package:syncache_flutter/syncache_flutter.dart'; |
| 22 | +
|
| 23 | +void main() async { |
| 24 | + WidgetsFlutterBinding.ensureInitialized(); |
| 25 | + |
| 26 | + // Initialize connectivity detection |
| 27 | + final network = FlutterNetwork(); |
| 28 | + await network.initialize(); |
| 29 | + |
| 30 | + // Create your cache |
| 31 | + final userCache = Syncache<User>( |
| 32 | + store: MemoryStore<User>(), |
| 33 | + network: network, |
| 34 | + ); |
| 35 | +
|
| 36 | + runApp( |
| 37 | + SyncacheScope<User>( |
| 38 | + cache: userCache, |
| 39 | + network: network, |
| 40 | + child: MyApp(), |
| 41 | + ), |
| 42 | + ); |
| 43 | +} |
| 44 | +``` |
| 45 | + |
| 46 | +### 2. Display cached data with CacheBuilder |
| 47 | + |
| 48 | +Use `CacheBuilder` to reactively display cached data: |
| 49 | + |
| 50 | +```dart |
| 51 | +class UserProfile extends StatelessWidget { |
| 52 | + final String userId; |
| 53 | + |
| 54 | + const UserProfile({required this.userId}); |
| 55 | +
|
| 56 | + @override |
| 57 | + Widget build(BuildContext context) { |
| 58 | + return CacheBuilder<User>( |
| 59 | + cacheKey: 'user:$userId', |
| 60 | + fetch: (request) => api.getUser(userId), |
| 61 | + builder: (context, snapshot) { |
| 62 | + if (snapshot.hasError) { |
| 63 | + return ErrorWidget(snapshot.error!); |
| 64 | + } |
| 65 | + if (!snapshot.hasData) { |
| 66 | + return const CircularProgressIndicator(); |
| 67 | + } |
| 68 | + return Text('Hello, ${snapshot.data!.name}'); |
| 69 | + }, |
| 70 | + ); |
| 71 | + } |
| 72 | +} |
| 73 | +``` |
| 74 | + |
| 75 | +## Features |
| 76 | + |
| 77 | +### SyncacheScope |
| 78 | + |
| 79 | +Provides cache instances to the widget subtree via `InheritedWidget`: |
| 80 | + |
| 81 | +```dart |
| 82 | +// Access cache anywhere in the subtree |
| 83 | +final cache = SyncacheScope.of<User>(context); |
| 84 | +
|
| 85 | +// Access the lifecycle observer |
| 86 | +final observer = SyncacheScope.observerOf<User>(context); |
| 87 | +``` |
| 88 | + |
| 89 | +### MultiSyncacheScope |
| 90 | + |
| 91 | +Provide multiple cache types without deep nesting: |
| 92 | + |
| 93 | +```dart |
| 94 | +MultiSyncacheScope( |
| 95 | + network: flutterNetwork, |
| 96 | + configs: [ |
| 97 | + SyncacheScopeConfig<User>(userCache), |
| 98 | + SyncacheScopeConfig<Post>(postCache), |
| 99 | + SyncacheScopeConfig<Settings>(settingsCache), |
| 100 | + ], |
| 101 | + child: MyApp(), |
| 102 | +) |
| 103 | +``` |
| 104 | + |
| 105 | +### CacheBuilder |
| 106 | + |
| 107 | +StreamBuilder-style widget for reactive cache display: |
| 108 | + |
| 109 | +```dart |
| 110 | +CacheBuilder<User>( |
| 111 | + cacheKey: 'user:123', |
| 112 | + fetch: fetchUser, |
| 113 | + policy: Policy.staleWhileRefresh, |
| 114 | + ttl: Duration(minutes: 5), |
| 115 | + initialData: cachedUser, |
| 116 | + buildWhen: (previous, current) => previous.id != current.id, |
| 117 | + builder: (context, snapshot) { |
| 118 | + // Build UI based on snapshot state |
| 119 | + }, |
| 120 | +) |
| 121 | +``` |
| 122 | + |
| 123 | +### CacheConsumer |
| 124 | + |
| 125 | +Consumer pattern with separate listener for side effects: |
| 126 | + |
| 127 | +```dart |
| 128 | +CacheConsumer<User>( |
| 129 | + cacheKey: 'user:123', |
| 130 | + fetch: fetchUser, |
| 131 | + listener: (context, data) { |
| 132 | + // Handle side effects (e.g., show snackbar, navigate) |
| 133 | + ScaffoldMessenger.of(context).showSnackBar( |
| 134 | + SnackBar(content: Text('User updated: ${data.name}')), |
| 135 | + ); |
| 136 | + }, |
| 137 | + builder: (context, snapshot) { |
| 138 | + // Build UI |
| 139 | + }, |
| 140 | +) |
| 141 | +``` |
| 142 | + |
| 143 | +### FlutterNetwork |
| 144 | + |
| 145 | +Connectivity detection using `connectivity_plus`: |
| 146 | + |
| 147 | +```dart |
| 148 | +final network = FlutterNetwork( |
| 149 | + debounceDuration: Duration(milliseconds: 500), |
| 150 | +); |
| 151 | +await network.initialize(); |
| 152 | +
|
| 153 | +// Check current status |
| 154 | +print('Online: ${network.isOnline}'); |
| 155 | +
|
| 156 | +// Listen to connectivity changes |
| 157 | +network.onConnectivityChanged.listen((isOnline) { |
| 158 | + print('Connectivity changed: $isOnline'); |
| 159 | +}); |
| 160 | +``` |
| 161 | + |
| 162 | +### Lifecycle Management |
| 163 | + |
| 164 | +Configure automatic refetching on app resume and connectivity restoration: |
| 165 | + |
| 166 | +```dart |
| 167 | +SyncacheScope<User>( |
| 168 | + cache: userCache, |
| 169 | + network: network, |
| 170 | + config: LifecycleConfig( |
| 171 | + refetchOnResume: true, |
| 172 | + refetchOnResumeMinDuration: Duration(minutes: 1), |
| 173 | + refetchOnReconnect: true, |
| 174 | + onRefetchError: (key, error, stackTrace) { |
| 175 | + logger.warning('Failed to refetch $key: $error'); |
| 176 | + }, |
| 177 | + ), |
| 178 | + child: MyApp(), |
| 179 | +) |
| 180 | +``` |
| 181 | + |
| 182 | +### SyncacheValueListenable |
| 183 | + |
| 184 | +Use with `ValueListenableBuilder` for more control: |
| 185 | + |
| 186 | +```dart |
| 187 | +final listenable = cache.toValueListenable( |
| 188 | + key: 'user:123', |
| 189 | + fetch: fetchUser, |
| 190 | +); |
| 191 | +
|
| 192 | +ValueListenableBuilder<AsyncSnapshot<User>>( |
| 193 | + valueListenable: listenable, |
| 194 | + builder: (context, snapshot, child) { |
| 195 | + // Build UI |
| 196 | + }, |
| 197 | +) |
| 198 | +
|
| 199 | +// Trigger manual refresh |
| 200 | +await listenable.refresh(); |
| 201 | +
|
| 202 | +// Don't forget to dispose |
| 203 | +listenable.dispose(); |
| 204 | +``` |
| 205 | + |
| 206 | +## API Reference |
| 207 | + |
| 208 | +### Widgets |
| 209 | + |
| 210 | +| Widget | Description | |
| 211 | +|--------|-------------| |
| 212 | +| `SyncacheScope<T>` | InheritedWidget for cache dependency injection | |
| 213 | +| `MultiSyncacheScope` | Provides multiple cache types without nesting | |
| 214 | +| `CacheBuilder<T>` | StreamBuilder-style reactive cache display | |
| 215 | +| `CacheConsumer<T>` | Consumer pattern with listener callback | |
| 216 | + |
| 217 | +### Classes |
| 218 | + |
| 219 | +| Class | Description | |
| 220 | +|-------|-------------| |
| 221 | +| `FlutterNetwork` | Connectivity detection with debouncing | |
| 222 | +| `SyncacheLifecycleObserver<T>` | App lifecycle and reconnect handling | |
| 223 | +| `LifecycleConfig` | Configuration for lifecycle behavior | |
| 224 | +| `SyncacheValueListenable<T>` | ValueListenable wrapper for cache streams | |
| 225 | +| `WatcherRegistration<T>` | Registration info for lifecycle-based refetching | |
| 226 | + |
| 227 | +## Requirements |
| 228 | + |
| 229 | +- Dart SDK: ^3.0.0 |
| 230 | +- Flutter: >=3.10.0 |
| 231 | +- syncache: ^0.1.0 |
| 232 | +- connectivity_plus: ^7.0.0 |
| 233 | + |
| 234 | +## License |
| 235 | + |
| 236 | +MIT License - see [LICENSE](LICENSE) for details. |
0 commit comments