sqflite-common-ffi-testing

Test sqflite database code on Dart VM and Flutter test runners using sqflite_common_ffi.

3.0k|555|Updated May 22, 2017
One-click install
npx skills add https://github.com/tekartik/sqflite --skill sqflite-common-ffi-testing-tekartik
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: sqflite-common-ffi-testing
Source: https://github.com/tekartik/sqflite/tree/main/sqflite_common_ffi/skills/sqflite-common-ffi-testing
Command: npx skills add https://github.com/tekartik/sqflite --skill sqflite-common-ffi-testing-tekartik

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill requires sqflite_common_ffi, sqlite3.

What problem does it solve? The native sqflite plugin cannot run in dart test or flutter test because it requires an iOS/Android device or emulator. This Skill shows how to use package:sqflite_common_ffi to run real SQLite databases on the developer machine and CI runners, so database code is tested against SQLite itself instead of mocks. ## Core Features & Use Cases - FFI-based test factory: Initialize sqfliteFfiInit() and open inMemoryDatabasePath with databaseFactoryFfi for fast, isolated unit tests. - Global factory replacement: Set databaseFactory = databaseFactoryFfi so existing code calling the global openDatabase() works unchanged in flutter test. - Widget test support: Use databaseFactoryFfiNoIsolate inside testWidgets to avoid hangs caused by the fake async zone of WidgetTester. - Use Case: Write a migration test that opens a file-based database at version 1, reopens it at version 2 with onUpgrade, and asserts the new column exists—all on a Linux CI runner with no emulator. ## Quick Start Write a Dart unit test that initializes sqfliteFfiInit, opens an in-memory database with databaseFactoryFfi, inserts a row, and asserts the query result.

Frequently Asked Questions about sqflite-common-ffi-testing

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I test sqflite code in flutter test without an emulator?▼

Add sqflite_common_ffi to dev_dependencies, call sqfliteFfiInit() in setUpAll, and set databaseFactory = databaseFactoryFfi. Existing code using the global openDatabase() then runs against real SQLite on the test machine.

How do I use sqflite in Flutter widget tests?▼

Use databaseFactoryFfiNoIsolate instead of databaseFactoryFfi in testWidgets. The isolate-based factory can hang under the fake async zone of WidgetTester, while the no-isolate factory runs SQLite synchronously in the test isolate.

Does sqflite_common_ffi work on CI runners like GitHub Actions?▼

Yes, Linux, macOS, and Windows runners all work with sqlite3 version 3 or later without extra packages, since SQLite is bundled through build hooks. Only the legacy sqlite3 v2 setup required installing libsqlite3-dev on Ubuntu.

Why do my sqflite tests fail on Windows but pass on macOS?▼

The usual cause is a missing sqfliteFfiInit() call, which is required on Windows but harmless elsewhere. Call it once at the top of main() or in setUpAll before opening any database.

How do I assert on sqflite DatabaseException errors in tests?▼

Catch DatabaseException and use its helper methods such as isNoSuchTableError('Test'), isUniqueConstraintError('Test.value'), or isSyntaxError(). Never match on exception message text, as messages are not stable.

Why does my test see stale data from a previous test?▼

Databases with the same path are returned as single instances, so an unclosed database leaks state between tests. Close the database in tearDown, prefer inMemoryDatabasePath, or call deleteDatabase in setUp for file-based tests.