Skip to content

Commit

Permalink
Try fix db access on android
Browse files Browse the repository at this point in the history
  • Loading branch information
felix1801 committed Sep 2, 2024
1 parent 7177317 commit a08ea53
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 17 deletions.
38 changes: 21 additions & 17 deletions lib/utils/flashcards_collection.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'package:path_provider/path_provider.dart';
import 'package:path/path.dart';
import 'package:sqflite_common_ffi/sqflite_ffi.dart';
import 'flashcard.dart';
import 'package:sqflite/sqflite.dart';

class FlashcardsCollection {
static const String _dbName = 'flashcards.db';
Expand All @@ -17,25 +18,28 @@ class FlashcardsCollection {

Future<void> _initDatabase() async {
// Initialize the database
sqfliteFfiInit(); // Initialize the ffi loader, essential ?
databaseFactory =
databaseFactoryFfi; // Set the database factory to ffi, essential ?

String dbPath;
if (Platform.isLinux) {
dbPath = _dbName;
if (Platform.isAndroid || Platform.isIOS) {
// Use the default SQLite implementation for mobile platforms
String dbPath = join((await getApplicationDocumentsDirectory()).path, _dbName);
_database = await openDatabase(
dbPath,
version: _dbVersion,
onCreate: _createDatabase,
);
} else {
final appDocDir = await getApplicationDocumentsDirectory();
dbPath = join(appDocDir.path, _dbName);
// Use FFI for desktop platforms
sqfliteFfiInit();
databaseFactory = databaseFactoryFfi;
String dbPath = Platform.isLinux ? _dbName : join((await getApplicationDocumentsDirectory()).path, _dbName);
_database = await databaseFactoryFfi.openDatabase(
dbPath,
options: OpenDatabaseOptions(
version: _dbVersion,
onCreate: _createDatabase,
),
);
}
print(dbPath); // Print the path of the database (for debugging purposes
// Open the database
_database = await openDatabase(
dbPath,
version: _dbVersion,
onCreate: _createDatabase,
);
}
print('Database initialized at: ${_database.path}'); }

Future<void> _createDatabase(Database db, int version) async {
// Create the database if it doesn't exist
Expand Down
2 changes: 2 additions & 0 deletions macos/Flutter/GeneratedPluginRegistrant.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import FlutterMacOS
import Foundation

import path_provider_foundation
import sqflite
import sqlite3_flutter_libs
import url_launcher_macos

func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
PathProviderPlugin.register(with: registry.registrar(forPlugin: "PathProviderPlugin"))
SqflitePlugin.register(with: registry.registrar(forPlugin: "SqflitePlugin"))
Sqlite3FlutterLibsPlugin.register(with: registry.registrar(forPlugin: "Sqlite3FlutterLibsPlugin"))
UrlLauncherPlugin.register(with: registry.registrar(forPlugin: "UrlLauncherPlugin"))
}
8 changes: 8 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.10.0"
sqflite:
dependency: "direct main"
description:
name: sqflite
sha256: a43e5a27235518c03ca238e7b4732cf35eabe863a369ceba6cbefa537a66f16d
url: "https://pub.dev"
source: hosted
version: "2.3.3+1"
sqflite_common:
dependency: transitive
description:
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ dependencies:
# path: ^1.8.3 # Added fom a youtube video : https://www.youtube.com/watch?v=sC_qC0QrQ90
sqlite3_flutter_libs: ^0.5.24 # https://stackoverflow.com/questions/65551415/failed-to-load-dynamic-library-in-flutter-app
# flutter_dotenv: ^5.1.0
sqflite: ^2.3.3+1

http: ^1.2.2
intl: ^0.19.0
Expand Down

0 comments on commit a08ea53

Please sign in to comment.