Cross-platform definitions topic

Definitions and common interfaces that are implemented by both the native and the web-specific bindings to SQLite.

Restricting your usage of the sqlite3 package to package:sqlite3/common.dart means that your code can run on all platforms. However, this doesn't give you access to the actual implementations that are part of the native and web implementations. You can write most of your database code with the common definitions and then use conditional imports to make an implementation available. For this, create three files:

  1. database_stub.dart.
  2. database_native.dart.
  3. datbaase_web.dart.

The content of these files depends on your needs, but could look like this:

// database_stub.dart
import 'package:sqlite3/common.dart';

Future<CommonDatabase> openDatabase() async {
  throw UnsupportedError('Unknown platform');
}
// database_native.dart
import 'package:sqlite3/sqlite3.dart';

Future<CommonDatabase> openDatabase() async {
  final path = await pickDatabasePath(); // e.g. with package:path_provider
  return sqlite3.open(path);
}
// database_web.dart
import 'package:sqlite3/wasm.dart';

Future<CommonDatabase> openDatabase() async {
  final sqlite = await WasmSqlite3.loadFromUrl(Uri.parse('sqlite3.wasm'));
  final fs = await IndexedDbFileSystem.open(dbName: 'app.db');
  sqlite.registerVirtualFileSystem(fs, makeDefault: true);
  return sqlite.open('/app.db');
}

With those files, you can use a conditional imports to support both web and native platforms:

import 'database_stub.dart'
    if (dart.library.io) 'database_native.dart'
    if (dart.library.js_interop) 'database_web.dart';

Libraries

common Cross-platform definitions
Exports common interfaces that are implemented by both the dart:ffi and the dart:js WASM version of this library.

Classes

AggregateContext<V> Cross-platform definitions
Application-defined context used to compute results in aggregate functions.
AggregateFunction<V> Cross-platform definitions
Interface for application-defined aggregate functions.
AllowedArgumentCount Cross-platform definitions
Describes how many arguments an application-defined sql function can take.
BaseVfsFile Cross-platform definitions
A VirtualFileSystemFile base class that implements xRead to zero-fill the buffer in case of short reads.
BaseVirtualFileSystem Cross-platform definitions
A VirtualFileSystem implementation that uses a Random instance for xRandomness and DateTime.now for xCurrentTime.
Changeset Cross-platform definitions
A changeset, representing changes made while a Session is active.
ChangesetOperation Cross-platform definitions
A recorded operation on a changeset.
CommonDatabase Cross-platform definitions
An opened sqlite3 database.
CommonPreparedStatement Cross-platform definitions
A prepared statement.
CommonSqlite3 Cross-platform definitions
Provides access to sqlite3 functions, such as opening new databases.
Cursor Cross-platform definitions
Base class for result sets.
CustomStatementParameter Cross-platform definitions
A parameter passed to prepared statements that decides how it gets mapped to SQL in applyTo.
DatabaseConfig Cross-platform definitions
Make configuration changes to the database connection.
InMemoryFileSystem Cross-platform definitions
A virtual file system implementation that stores all files in memory.
IteratingCursor Cross-platform definitions
A Cursor that can only be read once, obtaining rows from the database "on the fly" as moveNext is called.
Patchset Cross-platform definitions
A patchset obtained from a byte sequence or an active Session.
ResultSet Cross-platform definitions
Stores the full result of a select statement.
Row Cross-platform definitions
A single row in the result of a select statement.
Session Cross-platform definitions
A Session tracks database changes made by a Conn. It is used to build changesets.
SqlError Cross-platform definitions
Common result codes, https://www.sqlite.org/rescode.html Result Codes
SqlExtendedError Cross-platform definitions
Extended Result Codes, https://www.sqlite.org/rescode.html
SqlFileLockingLevels Cross-platform definitions
File lock levels, https://www.sqlite.org/c3ref/c_lock_exclusive.html
SqlFlag Cross-platform definitions
Flags for file open operations, https://www.sqlite.org/c3ref/c_open_autoproxy.html Flags For File Open Operations
SqlFunctionFlag Cross-platform definitions
Function flags, https://www.sqlite.org/c3ref/c_deterministic.html
Sqlite3Filename Cross-platform definitions
A filename passed to VirtualFileSystem.xOpen.
SqliteArguments Cross-platform definitions
Arguments passed to a user-defined SQLite function.
SqliteUpdate Cross-platform definitions
A data change notification from sqlite.
SqlPrepareFlag Cross-platform definitions
Prepare flags, https://www.sqlite.org/c3ref/c_prepare_normalize.html
SqlSpecialDestructor Cross-platform definitions
Special destructors, https://www.sqlite.org/c3ref/c_static.html
SqlTextEncoding Cross-platform definitions
Text Encodings, https://www.sqlite.org/c3ref/c_any.html These constant define integer codes that represent the various text encodings supported by SQLite.
SqlType Cross-platform definitions
Datatypes, https://sqlite.org/c3ref/c_blob.html
StatementParameters Cross-platform definitions
A set of values that can be used to bind parameters in a SQL query.
VirtualFileSystem Cross-platform definitions
A virtual filesystem used by sqlite3 to access the current I/O environment.
VirtualFileSystemFile Cross-platform definitions
A file implemented by a VFS author and returned by VirtualFileSystem.xOpen.
WindowFunction<V> Cross-platform definitions
A window function for sqlite3.

Constants

jsonb → const Codec<Object?, Uint8List> Cross-platform definitions
A Codec capable of converting Dart objects from and to the JSONB format used by sqlite3.

Enums

SqliteUpdateKind Cross-platform definitions
The kind of an SqliteUpdate received through a CommonDatabase.updates stream.

Exceptions / Errors

SqliteException Cross-platform definitions
Thrown by sqlite methods.
VfsException Cross-platform definitions
An exception thrown by VirtualFileSystem implementations written in Dart to signal that an operation could not be completed.