mirror of https://github.com/AMT-Cheif/drift.git
Highlight Android workarounds more
This commit is contained in:
parent
ca84c194bd
commit
8077c564d7
|
@ -3,3 +3,7 @@ import 'dart:io';
|
||||||
Future<Directory> getApplicationDocumentsDirectory() {
|
Future<Directory> getApplicationDocumentsDirectory() {
|
||||||
throw UnsupportedError('stub!');
|
throw UnsupportedError('stub!');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Future<Directory> getTemporaryDirectory() {
|
||||||
|
throw UnsupportedError('stub!');
|
||||||
|
}
|
||||||
|
|
|
@ -0,0 +1,3 @@
|
||||||
|
Future<void> applyWorkaroundToOpenSqlite3OnOldAndroidVersions() async {
|
||||||
|
throw 'stub!';
|
||||||
|
}
|
|
@ -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
|
|
@ -10,6 +10,8 @@ import 'dart:io';
|
||||||
import 'package:drift/native.dart';
|
import 'package:drift/native.dart';
|
||||||
import 'package:path_provider/path_provider.dart';
|
import 'package:path_provider/path_provider.dart';
|
||||||
import 'package:path/path.dart' as p;
|
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
|
// ... the TodoItems table definition stays the same
|
||||||
// #enddocregion open
|
// #enddocregion open
|
||||||
|
@ -47,6 +49,18 @@ LazyDatabase _openConnection() {
|
||||||
// for your app.
|
// for your app.
|
||||||
final dbFolder = await getApplicationDocumentsDirectory();
|
final dbFolder = await getApplicationDocumentsDirectory();
|
||||||
final file = File(p.join(dbFolder.path, 'db.sqlite'));
|
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);
|
return NativeDatabase.createInBackground(file);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -96,6 +96,11 @@ now looks like this:
|
||||||
|
|
||||||
{% include "blocks/snippet" snippets = snippets name = 'open' %}
|
{% 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
|
## Next steps
|
||||||
|
|
||||||
Congratulations! With this setup complete, your project is ready to use drift.
|
Congratulations! With this setup complete, your project is ready to use drift.
|
||||||
|
|
|
@ -19,9 +19,11 @@ dependencies:
|
||||||
# used in snippets
|
# used in snippets
|
||||||
http: ^1.1.0
|
http: ^1.1.0
|
||||||
sqlite3: ^2.0.0
|
sqlite3: ^2.0.0
|
||||||
# Fake path_provider for snippets
|
# Fake flutter packages for snippets
|
||||||
path_provider:
|
path_provider:
|
||||||
path: assets/path_provider
|
path: assets/path_provider
|
||||||
|
sqlite3_flutter_libs:
|
||||||
|
path: assets/sqlite3_flutter_libs
|
||||||
# Used in examples
|
# Used in examples
|
||||||
rxdart: ^0.27.3
|
rxdart: ^0.27.3
|
||||||
yaml: ^3.1.1
|
yaml: ^3.1.1
|
||||||
|
|
|
@ -48,6 +48,10 @@ android {
|
||||||
targetSdkVersion flutter.targetSdkVersion
|
targetSdkVersion flutter.targetSdkVersion
|
||||||
versionCode flutterVersionCode.toInteger()
|
versionCode flutterVersionCode.toInteger()
|
||||||
versionName flutterVersionName
|
versionName flutterVersionName
|
||||||
|
|
||||||
|
ndk {
|
||||||
|
abiFilters 'armeabi-v7a', 'arm64-v8a', 'x86_64'
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
buildTypes {
|
buildTypes {
|
||||||
|
|
|
@ -6,6 +6,8 @@ import 'package:drift_dev/api/migrations.dart';
|
||||||
import 'package:flutter/foundation.dart';
|
import 'package:flutter/foundation.dart';
|
||||||
import 'package:path_provider/path_provider.dart';
|
import 'package:path_provider/path_provider.dart';
|
||||||
import 'package:path/path.dart' as p;
|
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 {
|
Future<File> get databaseFile async {
|
||||||
// We use `path_provider` to find a suitable path to store our data in.
|
// 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.
|
/// Obtains a database connection for running drift in a Dart VM.
|
||||||
DatabaseConnection connect() {
|
DatabaseConnection connect() {
|
||||||
return DatabaseConnection.delayed(Future(() async {
|
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,
|
||||||
|
);
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue