π Quick Start
ποΈ Setup
Section titled βποΈ SetupβTo setup blocsync, start by configuring
- Local caching
- Remote caching
- Authentication (optional)
import 'package:blocsync/blocsync.dart';import 'package:firebase_auth/firebase_auth.dart';
void main() async { WidgetsFlutterBinding.ensureInitialized();
BlocSyncConfig.localStorage = LocalStorage BlocSyncConfig.apiClient = ApiClient( apiKey: 'your_api_key', ); BlocSyncConfig.authProvider = FirebaseAuthProvider();
runApp(MyApp());}
Basic Usage
Section titled βBasic UsageβAll you need to do is extend SyncedBloc
instead of Bloc
and implement
toJson
and fromJson
methods for your state. Itβs similar to hydrated_bloc in
that way.
import 'package:blocsync/blocsync.dart';
class CounterBloc extends SyncedBloc<CounterEvent, int> { CounterBloc() : super(0) { on<Increment>((event, emit) => emit(state + 1)); on<Decrement>((event, emit) => emit(state - 1)); }
@override int fromJson(Map<String, dynamic> json) => json['value'] as int;
@override Map<String, dynamic>? toJson(int state) => {'value': state};}
π Hosting Options
Section titled βπ Hosting OptionsβYou have two options for running the sync server:
- Managed hosting: Use https://blocsync.dev for hassle-free cloud hosting
- Self-hosted: Run your own server using the included server package
π Authentication & Privacy π
Section titled βπ Authentication & Privacy πβBlocs can be configured as either private (per user) or public:
- Public blocs: No authentication required, state is shared across all users
- Private blocs: Require user authentication to ensure each userβs state stays private
For private blocs, we support multiple authentication providers:
- Firebase Auth
- Supabase
- Auth0
- Custom authentication solutions