Line data Source code
1 : import 'dart:io';
2 :
3 : import 'package:drift_flutter/drift_flutter.dart';
4 : import 'package:path/path.dart' as p;
5 : import 'package:path_provider/path_provider.dart';
6 :
7 : /// Returns native database options that resolve to sqflite-compatible paths.
8 0 : DriftNativeOptions? nativeDatabaseOptions(String name) {
9 0 : return DriftNativeOptions(
10 0 : databasePath: () => _resolveDatabasePath(name),
11 : );
12 : }
13 :
14 : // Matches sqflite's legacy path so existing databases are found on upgrade.
15 : // Android: sqflite uses `<appData>/databases/`, not path_provider's
16 : // `<appData>/app_flutter/`.
17 : // iOS/macOS: both use the Documents directory — no mismatch.
18 0 : Future<String> _resolveDatabasePath(String name) async {
19 0 : final documentsDir = await getApplicationDocumentsDirectory();
20 0 : if (Platform.isAndroid) {
21 0 : final dbDir = Directory(p.join(documentsDir.parent.path, 'databases'));
22 0 : await dbDir.create(recursive: true);
23 0 : return p.join(dbDir.path, name);
24 : }
25 0 : return p.join(documentsDir.path, name);
26 : }
|