Loading...
Loading...
Implements caching strategies for Flutter apps to improve performance and offline support. Use when retaining app data locally to reduce network requests or speed up startup.
npx skill4agent add flutter/skills flutter-caching-datashared_preferencessqflitepath_providerFlutterEngineStream<UserProfile> getUserProfile() async* {
// 1. Yield local cache first
final localProfile = await _databaseService.fetchUserProfile();
if (localProfile != null) yield localProfile;
// 2. Fetch remote, update cache, yield fresh data
try {
final remoteProfile = await _apiClientService.getUserProfile();
await _databaseService.updateUserProfile(remoteProfile);
yield remoteProfile;
} catch (e) {
// Handle network failure; UI already has local data
}
}synchronizedworkmanagerTimerpath_providergetApplicationDocumentsDirectory()getTemporaryDirectory()Future<File> get _localFile async {
final directory = await getApplicationDocumentsDirectory();
return File('${directory.path}/cache.txt');
}sqflitewhereArgsFuture<void> updateCachedRecord(Record record) async {
final db = await database;
await db.update(
'records',
record.toMap(),
where: 'id = ?',
whereArgs: [record.id], // NEVER use string interpolation here
);
}cached_network_imageImageProvidercreateStream()resolveStreamForKey()resolve()ImageCache.maxByteSizeImageCache.maxByteSizeImageCacheListViewGridViewViewportscrollCacheExtentScrollCacheExtentcacheExtentcacheExtentStyle// Correct implementation
ListView(
scrollCacheExtent: const ScrollCacheExtent.pixels(500.0),
children: // ...
)
Viewport(
scrollCacheExtent: const ScrollCacheExtent.viewport(0.5),
slivers: // ...
)operator ==Widgetoperator ==constFlutterEngineApplicationFlutterEngineCachewithCachedEngineFlutterActivityFlutterFragment// 1. Pre-warm in Application class
val flutterEngine = FlutterEngine(this)
flutterEngine.navigationChannel.setInitialRoute("/cached_route")
flutterEngine.dartExecutor.executeDartEntrypoint(DartEntrypoint.createDefault())
// 2. Cache the engine
FlutterEngineCache.getInstance().put("my_engine_id", flutterEngine)
// 3. Use in Activity/Fragment
startActivity(
FlutterActivity.withCachedEngine("my_engine_id").build(this)
)synchronizedfalseDatabaseServiceApiClientServiceRepositoryStream<T>synchronizedsynchronized == falseApplicationAndroidManifest.xmlFlutterEnginenavigationChannel.setInitialRoute()dartExecutor.executeDartEntrypoint()FlutterEngineCache.getInstance().put()FlutterActivityFlutterFragment.withCachedEngine("id")