Highlight Android workarounds more

This commit is contained in:
Simon Binder 2023-10-24 21:14:57 +02:00
parent ca84c194bd
commit 8077c564d7
No known key found for this signature in database
GPG Key ID: 7891917E4147B8C0
8 changed files with 54 additions and 2 deletions

View File

@ -3,3 +3,7 @@ import 'dart:io';
Future<Directory> getApplicationDocumentsDirectory() {
throw UnsupportedError('stub!');
}
Future<Directory> getTemporaryDirectory() {
throw UnsupportedError('stub!');
}

View File

@ -0,0 +1,3 @@
Future<void> applyWorkaroundToOpenSqlite3OnOldAndroidVersions() async {
throw 'stub!';
}

View File

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

View File

@ -10,6 +10,8 @@ import 'dart:io';
import 'package:drift/native.dart';
import 'package:path_provider/path_provider.dart';
import 'package:path/path.dart' as p;
import 'package:sqlite3/sqlite3.dart';
import 'package:sqlite3_flutter_libs/sqlite3_flutter_libs.dart';
// ... the TodoItems table definition stays the same
// #enddocregion open
@ -47,6 +49,18 @@ LazyDatabase _openConnection() {
// for your app.
final dbFolder = await getApplicationDocumentsDirectory();
final file = File(p.join(dbFolder.path, 'db.sqlite'));
// Also work around limitations on old Android versions
if (Platform.isAndroid) {
await applyWorkaroundToOpenSqlite3OnOldAndroidVersions();
final cachebase = (await getTemporaryDirectory()).path;
// We can't access /tmp on Android, which sqlite3 would try by default.
// Explicitly tell it about the correct temporary directory.
sqlite3.tempDirectory = cachebase;
}
return NativeDatabase.createInBackground(file);
});
}

View File

@ -96,6 +96,11 @@ now looks like this:
{% include "blocks/snippet" snippets = snippets name = 'open' %}
The Android-specific workarounds are necessary because sqlite3 attempts to use `/tmp` to store
private data on unix-like systems, which is forbidden on Android. We also use this opportunity
to work around a problem some older Android devices have with loading custom libraries through
`dart:ffi`.
## Next steps
Congratulations! With this setup complete, your project is ready to use drift.

View File

@ -19,9 +19,11 @@ dependencies:
# used in snippets
http: ^1.1.0
sqlite3: ^2.0.0
# Fake path_provider for snippets
# Fake flutter packages for snippets
path_provider:
path: assets/path_provider
sqlite3_flutter_libs:
path: assets/sqlite3_flutter_libs
# Used in examples
rxdart: ^0.27.3
yaml: ^3.1.1

View File

@ -48,6 +48,10 @@ android {
targetSdkVersion flutter.targetSdkVersion
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
ndk {
abiFilters 'armeabi-v7a', 'arm64-v8a', 'x86_64'
}
}
buildTypes {

View File

@ -6,6 +6,8 @@ import 'package:drift_dev/api/migrations.dart';
import 'package:flutter/foundation.dart';
import 'package:path_provider/path_provider.dart';
import 'package:path/path.dart' as p;
import 'package:sqlite3/sqlite3.dart';
import 'package:sqlite3_flutter_libs/sqlite3_flutter_libs.dart';
Future<File> get databaseFile async {
// We use `path_provider` to find a suitable path to store our data in.
@ -17,7 +19,19 @@ Future<File> get databaseFile async {
/// Obtains a database connection for running drift in a Dart VM.
DatabaseConnection connect() {
return DatabaseConnection.delayed(Future(() async {
return NativeDatabase.createBackgroundConnection(await databaseFile);
if (Platform.isAndroid) {
await applyWorkaroundToOpenSqlite3OnOldAndroidVersions();
final cachebase = (await getTemporaryDirectory()).path;
// We can't access /tmp on Android, which sqlite3 would try by default.
// Explicitly tell it about the correct temporary directory.
sqlite3.tempDirectory = cachebase;
}
return NativeDatabase.createBackgroundConnection(
await databaseFile,
);
}));
}