Skip to content

💧 On Device Caching

Blocsync uses the excellent hydrated_bloc package to cache your bloc states on device. SyncedBloc and LiveBloc both use HydratedMixin to cache your bloc states on device. This means that bloc data is loaded from the on-device storage first, and then fetched from the server.

import 'package:blocsync/blocsync.dart';
import 'package:path_provider/path_provider.dart';
BlocSyncConfig.storage = await HydratedStorage.build(
storageDirectory:
kIsWeb
? HydratedStorageDirectory.web
: HydratedStorageDirectory((await getTemporaryDirectory()).path),
);

If you don’t want to use on-device caching, instead of extending SyncedBloc or LiveBloc, extend Bloc directly and use the paired SyncedMixin or LiveMixin instead.

import 'package:blocsync/blocsync.dart';
class MyBloc extends Bloc<MyEvent, MyState> with SyncedMixin<MyState> {
MyBloc() : super(MyState.initial()) {
startSyncing();
}
}
import 'package:blocsync/blocsync.dart';
class MyBloc extends Bloc<MyEvent, MyState> with LiveMixin<MyEvent, MyState> {
MyBloc() : super(MyState.initial()) {
startListening();
}
}