Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DatabaseFactory not initialized when using sqflite in flutter

I followed the following tutorial on the official site for sqflite: https://pub.dev/packages/sqflite

But on this line

var databasesPath = await getDatabasesPath();

I get the following error:

Exception has occurred. StateError (Bad state: databaseFactory not initialized databaseFactory is only initialized when using sqflite. When using sqflite_common_ffi You must call databaseFactory = databaseFactoryFfi; before using global openDatabase API )

These are my dependencies:

dependecies:
  flutter:
    sdk: flutter
  sqflite: ^2.2.8
  path: ^1.8.2

I tried to open the Database without getting a path from getDatabasePath():

await openDatabase('demo.db', version: 1,...

But this throws the same error on openDatabase.

As I am not using sqflite_common_ffi I do not know what to do and would appreciate your help.

Edit: I now also tried to use the complete example on the end of the page docs.flutter.dev/cookbook/persistence/sqlite. I think it is some kind of configuration error, because the same project works on another machine of mine.

like image 565
Windschief Avatar asked Mar 06 '26 14:03

Windschief


1 Answers

dependencies:
   sqflite_common_ffi: any

//in main.dart write this:
import 'package:sqflite_common_ffi/sqflite_ffi.dart';
Future main() async {

// Initialize FFI
sqfliteFfiInit();


 databaseFactory = databaseFactoryFfi;
 runApp(MyApp());
}
like image 89
ِAbdullah Avatar answered Mar 08 '26 12:03

ِAbdullah



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!