sqflite_common_ffi 2.0.0+3
sqflite_common_ffi: ^2.0.0+3 copied to clipboard

sqflite ffi based implementation, for desktop and units tests.

sqflite ffi #

sqflite based ffi implementation. Based on sqlite3. Thanks to Simon Binder

Currently supported on Linux, MacOS and Windows on both Flutter and Dart VM.

While not tested extensively, it also works on iOS and Android (using sqlite3_flutter_libs - Thanks to Simon Binder)

It allows also mocking sqflite during regular flutter unit test (i.e. not using the emulator/simulator).

Getting Started #

Dart #

Add the following dev dependency:

dev_dependencies:
  sqflite_common_ffi:

Linux #

libsqlite3 and libsqlite3-dev linux packages are required.

One time setup for Ubuntu (to run as root):

dart tool/linux_setup.dart

or

sudo apt-get -y install libsqlite3-0 libsqlite3-dev

MacOS #

Should work as is.

Windows #

Should work as is in debug mode (sqlite3.dll is bundled).

In release mode, add sqlite3.dll in same folder as your executable.

sqfliteFfiInit is provided as an implementation reference for loading the sqlite library. Please look at sqlite3 if you want to override the behavior.

Sample code #

Unit test code #

sqflite_ffi_test.dart:

import 'package:test/test.dart';
import 'package:sqflite_common/sqlite_api.dart';
import 'package:sqflite_common_ffi/sqflite_ffi.dart';

void main() {
  // Init ffi loader if needed.
  sqfliteFfiInit();
  test('simple sqflite example', () async {
    var db = await databaseFactoryFfi.openDatabase(inMemoryDatabasePath);
    expect(await db.getVersion(), 0);
    await db.close();
  });
}

More info on unit testing here

Application #

Make it a normal dependency.

main.dart:

import 'package:sqflite_common/sqlite_api.dart';
import 'package:sqflite_common_ffi/sqflite_ffi.dart';

Future main() async {
  // Init ffi loader if needed.
  sqfliteFfiInit();

  var databaseFactory = databaseFactoryFfi;
  var db = await databaseFactory.openDatabase(inMemoryDatabasePath);
  await db.execute('''
  CREATE TABLE Product (
      id INTEGER PRIMARY KEY,
      title TEXT
  )
  ''');
  await db.insert('Product', <String, Object?>{'title': 'Product 1'});
  await db.insert('Product', <String, Object?>{'title': 'Product 1'});

  var result = await db.query('Product');
  print(result);
  // prints [{id: 1, title: Product 1}, {id: 2, title: Product 1}]
  await db.close();
}

If your existing application uses sqflite on iOS/Android/MacOS, you can also set the proper initialization to have your application work on Linux and windows.

Limitations #

  • Database calls are made in a separate isolate,
  • Multi-instance support (not common) is simulated
  • As another note, getDatabasesPath() has a lame implementation. You'd better rely on a custom strategy using package such as path_provider.
68
likes
130
pub points
96%
popularity

sqflite ffi based implementation, for desktop and units tests.

Homepage
Repository (GitHub)
View/report issues

Documentation

API reference

License

BSD-2-Clause (LICENSE)

Dependencies

meta, path, sqflite_common, sqlite3, synchronized

More

Packages that depend on sqflite_common_ffi