sqflite_common_ffi 1.0.0+4

  • Readme
  • Changelog
  • Example
  • Installing
  • 96

sqflite ffi #

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

It allows mocking sqflite during regular flutter unit test (i.e. not using the emulator/simulator). One goal is make it stricter than sqflite to encourage good practices.

Currently supported on Linux, MacOS and Windows.

Getting Started #

Dart #

Add the following dev dependency:

dev_dependencies:
  sqflite_common_ffi:

Linux #

sqlite3 and sqlite3-dev linux packages are required.

One time setup for Ubuntu:

dart tool/linux_setup.dart

MacOS #

Should work as is.

Windows #

Should work as is (sqlite3.dll is bundled).

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();
  });
}

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, dynamic>{'title': 'Product 1'});
  await db.insert('Product', <String, dynamic>{'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();
}

Limitations #

  • Primary intent was to support unit testing sqflite based code but the implementation works on Windows/Mac/Linux flutter desktop application
  • Database calls are made in a separate isolate,
  • Only Uint8List is accepted for blob since List<int> is not optimized
  • Multi-instance support (not common) is simulated

1.0.0+4 #

  • Support extended result code exception

1.0.0+1 #

  • Initial revision from sqflite_ffi_test experimentation

example/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, dynamic>{'title': 'Product 1'});
  await db.insert('Product', <String, dynamic>{'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();
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  sqflite_common_ffi: ^1.0.0+4

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:sqflite_common_ffi/sqflite_ffi.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
93
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
96
Learn more about scoring.

We analyzed this package on Jul 8, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.4
  • pana: 0.13.14

Analysis suggestions

Package does not support Flutter platform android

Because:

  • package:sqflite_common_ffi/sqflite_ffi.dart that imports:
  • package:sqflite_common_ffi/src/windows/setup.dart that imports:
  • package:moor_ffi/open_helper.dart that declares support for platforms:

Package does not support Flutter platform ios

Because:

  • package:sqflite_common_ffi/sqflite_ffi.dart that imports:
  • package:sqflite_common_ffi/src/windows/setup.dart that imports:
  • package:moor_ffi/open_helper.dart that declares support for platforms:

Package does not support Flutter platform linux

Because:

  • package:sqflite_common_ffi/sqflite_ffi.dart that imports:
  • package:sqflite_common_ffi/src/windows/setup.dart that imports:
  • package:moor_ffi/open_helper.dart that declares support for platforms:

Package does not support Flutter platform macos

Because:

  • package:sqflite_common_ffi/sqflite_ffi.dart that imports:
  • package:sqflite_common_ffi/src/windows/setup.dart that imports:
  • package:moor_ffi/open_helper.dart that declares support for platforms:

Package does not support Flutter platform web

Because:

  • package:sqflite_common_ffi/sqflite_ffi.dart that imports:
  • package:sqflite_common_ffi/src/windows/setup.dart that imports:
  • package:moor_ffi/open_helper.dart that declares support for platforms:

Package does not support Flutter platform windows

Because:

  • package:sqflite_common_ffi/sqflite_ffi.dart that imports:
  • package:sqflite_common_ffi/src/windows/setup.dart that imports:
  • package:moor_ffi/open_helper.dart that declares support for platforms:

Package not compatible with runtime js

Because:

  • package:sqflite_common_ffi/sqflite_ffi.dart that imports:
  • package:sqflite_common_ffi/src/windows/setup.dart that imports:
  • package:sqflite_common_ffi/src/windows/setup_impl.dart that imports:
  • dart:io

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.7.0 <3.0.0
meta >=1.1.8 <3.0.0 1.2.1
moor_ffi >=0.5.0 <1.0.0 0.7.0
path >=1.5.1 <3.0.0 1.7.0
sqflite_common >=1.0.1 <3.0.0 1.0.2+1
synchronized >=2.0.2 <4.0.0 2.2.0+1
Transitive dependencies
charcode 1.1.3
collection 1.14.13 1.15.0-nnbd
convert 2.1.1
ffi 0.1.3
moor 3.2.0
typed_data 1.2.0 1.3.0-nnbd
Dev dependencies
pedantic any 1.9.1
process_run >=0.10.7
test any