Skip to content

Commit 3089854

Browse files
committed
Fix importing and exporting docs for drift_flutter
Closes simolus3#3498
1 parent 8ea0617 commit 3089854

File tree

4 files changed

+91
-3
lines changed

4 files changed

+91
-3
lines changed

docs/docs/Examples/existing_databases.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@ flutter:
3838
3939
To initialize the database before using drift, you need to extract the asset from your
4040
app onto the device.
41-
In drift, you can use a [LazyDatabase](https://pub.dev/documentation/drift/latest/drift/LazyDatabase-class.html)
42-
to perform that work just before your drift database is opened:
4341
4442
```dart
4543
import 'package:drift/drift.dart';
@@ -79,6 +77,21 @@ LazyDatabase _openConnection() {
7977
}
8078
```
8179

80+
=== "Using `drift_flutter`"
81+
82+
{{ load_snippet('(full)','lib/snippets/examples/existing_databases_flutter.dart.excerpt.json',indent=4) }}
83+
84+
=== "Using `package:drift/native.dart`"
85+
86+
In drift, you can use a [LazyDatabase](https://pub.dev/documentation/drift/latest/drift/LazyDatabase-class.html)
87+
to perform that work just before your drift database is opened:
88+
89+
{{ load_snippet('(full)','lib/snippets/examples/existing_databases_native.dart.excerpt.json',indent=4) }}
90+
91+
92+
!!! warning
93+
This snippet only works on native platforms. See [existing databases on the web](../Platforms/web.md#using-existing-databases) for web support.
94+
8295
Finally, use that method to open your database:
8396

8497
```dart
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import 'dart:io';
2+
3+
import 'package:drift/drift.dart';
4+
5+
import 'package:drift_flutter/drift_flutter.dart';
6+
import 'package:flutter/services.dart' show rootBundle;
7+
import 'package:path/path.dart' as p;
8+
import 'package:path_provider/path_provider.dart';
9+
10+
QueryExecutor openConnection() {
11+
return driftDatabase(
12+
// By default, drift creates a file named app.sqlite based on the name here.
13+
// Because we need to interact with the underlying file, we add the
14+
// `databasePath` option to specify the exact path here.
15+
name: 'app',
16+
native: DriftNativeOptions(
17+
databasePath: () async {
18+
// put the database file, called db.sqlite here, into the documents
19+
// folder for your app.
20+
final dbFolder = await getApplicationDocumentsDirectory();
21+
final file = File(p.join(dbFolder.path, 'app.db'));
22+
23+
if (!await file.exists()) {
24+
// Extract the pre-populated database file from assets
25+
final blob = await rootBundle.load('assets/my_database.db');
26+
final buffer = blob.buffer;
27+
await file.writeAsBytes(
28+
buffer.asUint8List(blob.offsetInBytes, blob.lengthInBytes));
29+
}
30+
31+
return file.path;
32+
},
33+
),
34+
);
35+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import 'dart:io';
2+
3+
import 'package:drift/drift.dart';
4+
import 'package:drift/native.dart';
5+
import 'package:flutter/services.dart' show rootBundle;
6+
import 'package:path/path.dart' as p;
7+
import 'package:path_provider/path_provider.dart';
8+
import 'package:sqlite3/sqlite3.dart';
9+
import 'package:sqlite3_flutter_libs/sqlite3_flutter_libs.dart';
10+
11+
QueryExecutor openConnection() {
12+
return LazyDatabase(() async {
13+
// put the database file, called db.sqlite here, into the documents folder
14+
// for your app.
15+
final dbFolder = await getApplicationDocumentsDirectory();
16+
final file = File(p.join(dbFolder.path, 'app.db'));
17+
18+
if (!await file.exists()) {
19+
// Extract the pre-populated database file from assets
20+
final blob = await rootBundle.load('assets/my_database.db');
21+
final buffer = blob.buffer;
22+
await file.writeAsBytes(
23+
buffer.asUint8List(blob.offsetInBytes, blob.lengthInBytes));
24+
}
25+
26+
// Also work around limitations on old Android versions
27+
if (Platform.isAndroid) {
28+
await applyWorkaroundToOpenSqlite3OnOldAndroidVersions();
29+
}
30+
31+
// Make sqlite3 pick a more suitable location for temporary files - the
32+
// one from the system may be inaccessible due to sandboxing.
33+
final cachebase = (await getTemporaryDirectory()).path;
34+
// We can't access /tmp on Android, which sqlite3 would try by default.
35+
// Explicitly tell it about the correct temporary directory.
36+
sqlite3.tempDirectory = cachebase;
37+
38+
return NativeDatabase.createInBackground(file);
39+
});
40+
}

docs/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ dev_dependencies:
4545
webdev: ^3.6.0
4646
code_snippets:
4747
hosted: https://pub-simonbinder-eu.fsn1.your-objectstorage.com
48-
version: ^0.0.16
48+
version: ^0.0.18
4949
sass_builder: ^2.2.1
5050

5151
dependency_overrides:

0 commit comments

Comments
 (0)