Flutter apps
The bothy package wraps the Rust engine as a native
library. Building an app with it needs a Rust toolchain (rustup); the
native code is compiled for each platform during the Flutter build.
dependencies: bothy: ^0.4.0Open, write, watch
Section titled “Open, write, watch”import 'package:bothy/bothy.dart';
await Bothy.initialize();final db = await Bothy.open( path: '${supportDir.path}/notes.db', appId: 'com.example.notes', remote: BothyRemoteSpec.googleDriveAppData( (forceRefresh) => mySignIn.accessToken(refresh: forceRefresh), ), p2p: true,);await db.initOrJoin();
final notes = db.collection('notes');await notes.put('n1', {'title': 'Hello', 'done': false});await notes.patch('n1', {'done': true});final note = await notes.get('n1');
notes.watch().listen((change) => refresh(change.id));db.startAutoSync();Google sign-in
Section titled “Google sign-in”The package never signs in by itself. Your app runs the OAuth flow, for example with
google_sign_in on Android and iOS or a browser loopback on desktop, and gives Bothy a
callback that returns an access token:
- with the
drive.appdatascope forBothyRemoteSpec.googleDriveAppData; - or with the
drive.filescope forBothyRemoteSpec.googleDriveFolder(name).
Return null when the user is signed out. Bothy calls the callback with forceRefresh: true
after Drive rejects a token. Create the clients as in
Set up Google sign-in; on Android you need an Android client
(package name and signing SHA-1) and a Web client.
Encryption
Section titled “Encryption”final db = await Bothy.open(..., encrypt: true, password: password); // first devicefinal db = await Bothy.open(..., password: password); // other devicesKeep await db.exportKey() in flutter_secure_storage and open with key: next time. A
wrong password throws BothyException with BothyErrorKind.wrongPassword. A database opened
without a secret is locked: local reads and writes work, and sync throws
BothyErrorKind.keyRequired until unlock.
Background sync
Section titled “Background sync”startAutoSync() syncs while the app runs. For sync while the app is closed, schedule
db.sync() with WorkManager (Android) or BGTaskScheduler (iOS); the demo app shows both.
Direct channels only run while the app is in the foreground.
The demo app
Section titled “The demo app”examples/flutter-notes
is a complete notes app: sign-in on every platform, encryption, conflicts, a Robot screen and
direct channels. Its README covers the OAuth client setup.