sqflite_platform_interface
The platform interface of the sqflite
Flutter plugin: the base of a platform implementation of sqflite, like
sqflite_android and sqflite_darwin.
An application does not depend on it. It uses sqflite (Android, iOS, macOS), sqflite_common_ffi (Windows, Linux, Dart VM, unit tests) or sqflite_common_ffi_web (web).
Getting started
In your implementation package (a Flutter plugin package):
flutter pub add sqflite_platform_interface sqflite_common
If the native side of your package speaks the sqflite method channel protocol
(as the Android and iOS/macOS code does), register the method channel factory
from the registerWith() of a SqflitePlatform subclass:
import 'package:sqflite_platform_interface/sqflite_platform_interface.dart';
/// sqflite implementation for "myos".
class SqfliteMyOs extends SqflitePlatform {
/// Called by Flutter before `main()` (the `dartPluginClass`).
static void registerWith() {
SqflitePlatform.initWithDatabaseFactoryMethodChannel();
}
}
and declare it in its pubspec.yaml:
flutter:
plugin:
implements: sqflite
platforms:
myos:
pluginClass: SqflitePlugin
dartPluginClass: SqfliteMyOs
An app using sqflite then calls openDatabase() as usual on this platform.
What it provides
| API | Use it to |
|---|---|
SqflitePlatform.initWithDatabaseFactoryMethodChannel() |
set the method channel factory as the global databaseFactory (an already set factory is kept) |
SqflitePlatform.databaseFactoryMethodChannel |
get the method channel DatabaseFactory (channel com.tekartik.sqflite) |
SqflitePlatform().databaseFactory |
get or set the global databaseFactory of sqflite_common |
The method channel factory runs the shared sqflite Dart code of
sqflite_common (versions, transactions, batches, single instances) and sends
the SQL to the native side. The messages are described in
method_call_protocol.md.
readDatabaseBytes() and writeDatabaseBytes() do not go through the
channel: they read and write the file in Dart (dart:io), a relative path
being in the databases path, as for openDatabase().
A Dart implementation (ffi, web) does not need the channel: its
registerWith() sets its own factory, keeping one already set, as
sqflite_ffi does:
import 'package:sqflite_common/sqflite.dart';
/// [factory] is built with the sqflite_common helpers (for example
/// `databaseFactoryFfi` of sqflite_common_ffi).
void registerDartFactory(DatabaseFactory factory) {
databaseFactoryOrNull ??= factory;
}
Common mistakes
- Depending on this package from an app: use
sqflite. - No
dartPluginClassin the pubspec:registerWith()never runs andopenDatabase()throwsStateError: databaseFactory not initialized. - Native errors with a code other than
sqlite_error: the app gets aPlatformExceptioninstead of aDatabaseException. - Setting a class that only
implements DatabaseFactory: the setter throwsArgumentError(Unsupported sqflite factory). Build the factory with thesqflite_commonhelpers (buildDatabaseFactory).
Implementations
- sqflite_android (Android, example)
- sqflite_darwin (iOS and macOS, example)
- sqflite, the app-facing package, and its example
Libraries
- sqflite_platform_interface
- Platform interface of the sqflite Flutter plugin, for the platform
implementations (sqflite_android, sqflite_darwin...), not for apps (they
use
package:sqflite).