Skip to main content

sqflite_platform_interface 2.4.4 copy "sqflite_platform_interface: ^2.4.4" to clipboard
sqflite_platform_interface: ^2.4.4 copied to clipboard

Flutter plugin interface for SQLite, a self-contained, high-reliability, embedded, SQL database engine.

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.

pub package

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 dartPluginClass in the pubspec: registerWith() never runs and openDatabase() throws StateError: databaseFactory not initialized.
  • Native errors with a code other than sqlite_error: the app gets a PlatformException instead of a DatabaseException.
  • Setting a class that only implements DatabaseFactory: the setter throws ArgumentError (Unsupported sqflite factory). Build the factory with the sqflite_common helpers (buildDatabaseFactory).

Implementations #

2
likes
160
points
4.29M
downloads

Documentation

API reference

Publisher

verified publishertekartik.com

Weekly Downloads

Flutter plugin interface for SQLite, a self-contained, high-reliability, embedded, SQL database engine.

Repository (GitHub)
View/report issues

Topics

#sql #database

License

BSD-2-Clause (license)

Dependencies

flutter, meta, platform, plugin_platform_interface, sqflite_common

More

Packages that depend on sqflite_platform_interface