Back to Documentation
Quick Start
Get up and running with SyncLayer in less than 5 minutes
1
Installation
Add SyncLayer to your Flutter project's pubspec.yaml:
yaml
dependencies:
synclayer: ^0.2.0-beta.5Then run:
bash
flutter pub get2
Initialize SyncLayer
Initialize SyncLayer in your app's main function:
dart
import 'package:synclayer/synclayer.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await SyncLayer.init(
SyncConfig(
baseUrl: 'https://api.example.com',
collections: ['todos', 'users'],
conflictStrategy: ConflictStrategy.lastWriteWins,
),
);
runApp(MyApp());
}3
Perform CRUD Operations
Start using SyncLayer to save, retrieve, update, and delete data:
dart
// Create
final id = await SyncLayer
.collection('todos')
.save({'text': 'Buy milk', 'done': false});
// Read
final todo = await SyncLayer
.collection('todos')
.get(id);
// Update
await SyncLayer
.collection('todos')
.save({'done': true}, id: id);
// Delete
await SyncLayer
.collection('todos')
.delete(id);4
Watch for Changes
Listen to real-time updates from your local database:
dart
SyncLayer
.collection('todos')
.watch()
.listen((todos) {
print('Todos updated: ${todos.length} items');
// Update your UI
});5
Sync with Backend
Trigger manual sync or let automatic sync handle it:
dart
// Manual sync
await SyncLayer.syncNow();
// Listen to sync events
SyncLayer.events.listen((event) {
if (event is SyncStartedEvent) {
print('Sync started');
} else if (event is SyncCompletedEvent) {
print('Sync completed');
}
});You're all set!
Your app now has offline-first capabilities with automatic synchronization.