Update docs on sqlcipher flutter libs

This commit is contained in:
Simon Binder 2024-02-07 22:29:09 +01:00
parent eb5ff82be1
commit 882aad1e65
No known key found for this signature in database
GPG Key ID: 7891917E4147B8C0
6 changed files with 39 additions and 15 deletions

View File

@ -0,0 +1,9 @@
import 'dart:ffi';
Future<void> applyWorkaroundToOpenSqlCipherOnOldAndroidVersions() async {
throw 'stub!';
}
DynamicLibrary openCipherOnAndroid() {
throw 'stub';
}

View File

@ -0,0 +1,6 @@
name: sqlcipher_flutter_libs
publish_to: none
description: Fake "sqlcipher_flutter_libs" package so that we can import it in snippets without depending on Flutter.
environment:
sdk: ^2.16.0

View File

@ -160,7 +160,7 @@ targets:
global_options:
":api_index":
options:
packages: ['drift', 'drift_dev', 'sqlite3']
packages: ['drift', 'drift_dev', 'sqlite3', 'sqlite3_flutter_libs', 'sqlcipher_flutter_libs']
additional_public_assets:
- "pages/**"

View File

@ -1,15 +1,16 @@
import 'dart:io';
import 'package:drift/native.dart';
import 'package:drift_docs/snippets/isolates.dart';
import 'package:sqlite3/sqlite3.dart';
// #docregion setup
import 'dart:ffi';
import 'package:sqlite3/open.dart';
import 'package:sqlcipher_flutter_libs/sqlcipher_flutter_libs.dart';
// call this method before using drift
void setupSqlCipher() {
open.overrideFor(
OperatingSystem.android, () => DynamicLibrary.open('libsqlcipher.so'));
Future<void> setupSqlCipher() async {
await applyWorkaroundToOpenSqlCipherOnOldAndroidVersions();
open.overrideFor(OperatingSystem.android, openCipherOnAndroid);
}
// #enddocregion setup
@ -23,9 +24,13 @@ void databases() {
final myDatabaseFile = File('/dev/null');
// #docregion encrypted1
final token = RootIsolateToken.instance;
NativeDatabase.createInBackground(
myDatabaseFile,
isolateSetup: setupSqlCipher,
isolateSetup: () async {
BackgroundIsolateBinaryMessenger.ensureInitialized(token);
await setupSqlCipher();
},
setup: (rawDb) {
rawDb.execute("PRAGMA key = 'passphrase';");
},
@ -35,7 +40,10 @@ void databases() {
// #docregion encrypted2
NativeDatabase.createInBackground(
myDatabaseFile,
isolateSetup: setupSqlCipher,
isolateSetup: () async {
BackgroundIsolateBinaryMessenger.ensureInitialized(token);
await setupSqlCipher();
},
setup: (rawDb) {
assert(_debugCheckHasCipher(rawDb));
rawDb.execute("PRAGMA key = 'passphrase';");

View File

@ -62,7 +62,7 @@ To use `sqlcipher`, add a dependency on `sqlcipher_flutter_libs`:
```yaml
dependencies:
sqlcipher_flutter_libs: ^0.5.0
sqlcipher_flutter_libs: ^0.6.0
```
If you already have a dependency on `sqlite3_flutter_libs`, __drop that dependency__.
@ -77,14 +77,13 @@ of the regular `libsqlite3.so`:
When using drift on a background database, you need to call `setupSqlCipher` on the background isolate
as well. With `NativeDatabase.createInBackground`, which are using isolates internally, you can use
the `setupIsolate` callback to do this - the examples on this page use this as well.
Since `applyWorkaroundToOpenSqlCipherOnOldAndroidVersions()` invokes a platform channel, one needs
to install a `BackgroundIsolateBinaryMessenger` on the isolate as well.
On iOS and macOS, no additional setup is necessary - simply depend on `sqlcipher_flutter_libs`.
On Windows and Linux, you currently have to include a version of SQLCipher manually when you distribute
your app.
For more information on this, you can use the documentation [here]({{ '../Platforms/index.md#bundling-sqlite-with-your-app' | pageUrl }}).
Instead of including `sqlite3.dll` or `libsqlite3.so`, you'd include the respective versions
of SQLCipher.
On iOS, macOS and Windows, no additional setup is necessary - simply depend on `sqlcipher_flutter_libs`.
For Linux builds, note that OpenSSL is linked statically by default. If you want to compile your app to use
a dynamically-linked distribution of OpenSSL, see [this](https://github.com/simolus3/sqlite3.dart/issues/186#issuecomment-1742110933)
issue comment.
### Using

View File

@ -24,6 +24,8 @@ dependencies:
path: assets/path_provider
sqlite3_flutter_libs:
path: assets/sqlite3_flutter_libs
sqlcipher_flutter_libs:
path: assets/sqlcipher_flutter_libs
# Used in examples
rxdart: ^0.27.3
yaml: ^3.1.1