Add recommended setup to asset example

This commit is contained in:
Simon Binder 2023-12-14 22:09:37 +01:00
parent 5e51fbc1fc
commit ad856314c2
No known key found for this signature in database
GPG Key ID: 7891917E4147B8C0
1 changed files with 14 additions and 0 deletions

View File

@ -45,6 +45,8 @@ to perform that work just before your drift database is opened:
import 'package:drift/drift.dart';
import 'package:flutter/services.dart' show rootBundle;
import 'package:path/path.dart' as p;
import 'package:sqlite3/sqlite3.dart';
import 'package:sqlite3_flutter_libs/sqlite3_flutter_libs.dart';
LazyDatabase _openConnection() {
return LazyDatabase(() async {
@ -60,6 +62,18 @@ LazyDatabase _openConnection() {
await file.writeAsBytes(buffer.asUint8List(blob.offsetInBytes, blob.lengthInBytes));
}
// Also work around limitations on old Android versions
if (Platform.isAndroid) {
await applyWorkaroundToOpenSqlite3OnOldAndroidVersions();
}
// Make sqlite3 pick a more suitable location for temporary files - the
// one from the system may be inaccessible due to sandboxing.
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);
});
}