createDatabaseFactoryFfi function

DatabaseFactory createDatabaseFactoryFfi({
  1. SqfliteFfiInit? ffiInit,
  2. bool noIsolate = false,
  3. SqfliteFfiIsolatePortServer? isolatePortServer,
})

Creates an FFI database factory. Optionally the ffiInit function can be provided to run some code before SQLite is used (in the sqflite isolate, or in the calling isolate when noIsolate is true), once. This function should be either a top level function or a static function. If it throws, the database call fails with that error and the next call tries again. Prefer the use of the databaseFactoryFfi getter if you don't need this functionality.

All the ffi factories of a Dart isolate share one sqflite isolate, created by the first call with the ffiInit and isolatePortServer of the factory making it: the ones of the other factories are then not used.

With package:sqlite3 3.x (sqflite_common_ffi 2.4.0 and later), the SQLite library is not chosen in ffiInit but with build hooks options in the application pubspec.yaml, for example to use the system library:

hooks:
  user_defines:
    sqlite3:
      source: system

(package:sqlite3/open.dart used in ffiInit with sqlite3 v2 no longer exists.)

void ffiInit() {
  // Called before SQLite is used.
}

Future<void> main() async {
  final dbFactory = createDatabaseFactoryFfi(ffiInit: ffiInit);
  final db = await dbFactory.openDatabase(inMemoryDatabasePath);
  // ...
}

If isolatePortServer is provided (not supported on the web), it is used to share the sqflite isolate between isolates, typically using Flutter IsolateNameServer (see the sqflite_ffi package).

Implementation

DatabaseFactory createDatabaseFactoryFfi({
  SqfliteFfiInit? ffiInit,
  bool noIsolate = false,
  SqfliteFfiIsolatePortServer? isolatePortServer,
}) {
  return createDatabaseFactoryFfiImpl(
    ffiInit: ffiInit,
    noIsolate: noIsolate,
    isolatePortServer: isolatePortServer,
  );
}