Installation

Requirements

Our SDKs

Add dependency

dependencies:
  pocketsync_flutter: ^0.3.0

Basic setup

  1. Create a project in the PocketSync console
  2. Initialize PocketSync in your app:
import 'package:pocketsync_flutter/pocketsync_flutter.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  
  final dbPath = join(await getDatabasesPath(), 'my_app.db');
  
  try {
    await PocketSync.initialize(
      dbPath: dbPath,
      options: PocketSyncOptions(
        projectId: 'your-project-id',
        authToken: 'your-auth-token',
        serverUrl: 'https://api.pocketsync.dev',
      ),
      databaseOptions: databaseOptions,
    );

    PocketSync.instance.setUserId(userId: 'user-123');
    await PocketSync.instance.start();
  } catch (e) {
    print('Failed to initialize PocketSync: $e');
  }
}

Configuration

PocketSync can be configured with various options to customize its behavior. The PocketSyncOptions class accepts the following parameters:

ParameterDescriptionDefault
projectIdYour project ID from the PocketSync dashboardRequired
authTokenAuthentication token from the PocketSync dashboardRequired
serverUrlPocketSync server URLhttps://api.pocketsync.dev
changeLogRetentionDaysNumber of days to retain change logs30
syncExistingDataWhether to sync existing datatrue
conflictResolutionStrategyStrategy for resolving conflictslastWriteWins
customResolverCustom conflict resolver functionnull
verboseEnable detailed loggingfalse

Example configuration with custom options:

final options = PocketSyncOptions(
  projectId: 'your-project-id',
  authToken: 'your-auth-token',
  serverUrl: 'https://api.pocketsync.dev',
  changeLogRetentionDays: 60,
  syncExistingData: true,
  conflictResolutionStrategy: ConflictResolutionStrategy.custom,
  customResolver: (localChange, remoteChange) {
    // Your custom conflict resolution logic
    return localChange;
  },
  verbose: true,
);

Sync management

  • Call PocketSync.instance.start() to start the sync process.
  • Call PocketSync.instance.stop() to stop the sync process.
  • Make sure to call PocketSync.instance.setUserId(userId: 'user-123') before starting the sync process. This is required to identify the user and track their changes.

Device id generation

PocketSync will automatically generate a unique device identifier for your app. This identifier is used to track changes made by the user on this device. There are some known limitations with device id generation on iOS:

  • If the app is uninstalled and reinstalled, the device id will change.
  • If the app is deleted and reinstalled, the device id will change.
  • If the app is deleted and reinstalled, the device id will change.

Next Steps