sqflite-testing-and-platforms

Configure sqflite database factories for Flutter tests, desktop, web, and SQL debugging.

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

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill requires sqflite, sqflite_common_ffi, sqflite_common_ffi_web, sqflite_common, flutter_test.

What problem does it solve? Flutter's sqflite plugin only works on Android, iOS, and macOS, so flutter test fails with MissingPluginException and desktop or web builds have no database at all. This Skill shows how to swap the global databaseFactory so the same database code runs in unit tests, widget tests, Linux/Windows/Dart VM, and the web. ## Core Features & Use Cases - Test database setup: Initialize sqflite_common_ffi with sqfliteFfiInit and databaseFactoryFfi for unit tests, or databaseFactoryFfiNoIsolate for widget tests, using inMemoryDatabasePath for isolated test databases. - Cross-platform factory selection: Pick databaseFactoryFfiWeb for web, databaseFactoryFfi for desktop, and the plugin default on mobile, all from one main() using kIsWeb and Platform checks. - SQL debugging and diagnostics: Log every statement with SqfliteDatabaseFactoryLogger or debugQuickLoggerWrapper, tune lock warnings, enable Android WAL, and handle iOS unprotected folders and isolate constraints. - Use Case: Write a repository class that accepts a DatabaseFactory, then test migrations by opening a file database at version 1, reopening at version 2, and asserting the schema with PRAGMA table_info. ## Quick Start Set up my Flutter unit tests to run sqflite against an in-memory database using sqflite_common_ffi and show me how to select the right database factory per platform in main().

Frequently Asked Questions about sqflite-testing-and-platforms

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

FAQPage Schema
How do I unit test Flutter code that uses sqflite?▼

Add sqflite_common_ffi as a dev dependency, call sqfliteFfiInit() once, then set databaseFactory = databaseFactoryFfi before any openDatabase call. Use inMemoryDatabasePath for fast isolated tests, since the sqflite plugin itself throws MissingPluginException under flutter test.

How do I run sqflite in a Flutter widget test?▼

Use databaseFactoryFfiNoIsolate instead of databaseFactoryFfi in testWidgets, because the isolate-based factory hangs inside the widget test binding. Call sqfliteFfiInit() first, then open databases normally with openDatabase.

Can sqflite run on Windows, Linux, or the web?▼

Yes, via alternative factories: sqflite_common_ffi provides databaseFactoryFfi for Windows, Linux, and Dart VM, and sqflite_common_ffi_web provides databaseFactoryFfiWeb for browsers. Assign the factory once in main() before runApp, selecting with kIsWeb and Platform checks.

Why does sqflite throw MissingPluginException in tests?▼

MissingPluginException occurs because flutter test has no native SQLite and the sqflite plugin only registers on Android, iOS, and macOS. Fix it by pointing databaseFactory at databaseFactoryFfi from sqflite_common_ffi in your test setup.

How do I log every SQL statement in sqflite?▼

Wrap the factory with SqfliteDatabaseFactoryLogger from package:sqflite_common/sqflite_logger.dart to capture each statement, arguments, results, and duration. The one-liner databaseFactory.debugQuickLoggerWrapper() also works but is deprecated so it is not left in production.

What causes the sqflite database has been locked warning?▼

The 10-second locked warning almost always means a call was made on db inside db.transaction((txn) ...) instead of using the txn object. Use Sqflite.setLockWarningInfo to adjust the watchdog duration and callback.