import 'package:drift_wasm_example/database/dao/drift_actor_dao.dart';
import 'package:drift_wasm_example/database/drift_database.dart';
import 'package:drift_wasm_example/database/entities/drift_actor_entity.dart';
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
late final DriftDatabaseImpl _database;
late final DriftActorDao _actorDao;
@override
void initState() {
super.initState();
_initializeDatabase();
}
Future<void> _initializeDatabase() async {
print("initialize drift database");
_database = DriftDatabaseImpl();
_actorDao = _database.driftActorDao;
}
Future<void> _insertActors() async {
final generatedActors = List.generate(
100,
(index) => DriftActorEntity(
actorID: "$index",
parentActorID: "$index",
actorDescription: "Actor #$index description",
actorDistributorId: "$index",
actorTypeID: index,
actorStatus: index,
actorMachinesCount: index * 10,
),
);
await _actorDao.insertActors(generatedActors);
}
Future<void> _printActors() async {
final savedActors = await _actorDao.getActors();
for(final actor in savedActors) {
print(actor);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headlineMedium,
),
],
),
),
persistentFooterButtons: [
FloatingActionButton(
onPressed: _insertActors,
tooltip: 'Insert 100 actors',
child: const Icon(Icons.add),
),
const SizedBox(width: 20.0),
FloatingActionButton(
onPressed: _printActors,
tooltip: 'Print actors',
child: const Icon(Icons.print),
),
], // This trailing comma makes auto-formatting nicer for build methods.
);
}
}
DatabaseConnection connectOnWeb() {
return DatabaseConnection.delayed(Future(() async {
final result = await WasmDatabase.open(
databaseName: 'my_app_db', // prefer to only use valid identifiers here
sqlite3Uri: Uri.parse('sqlite3.wasm'),
driftWorkerUri: Uri.parse('drift_worker.js'),
);
if (result.missingFeatures.isNotEmpty) {
// Depending how central local persistence is to your app, you may want
// to show a warning to the user if only unrealiable implemetentations
// are available.
print('Using ${result.chosenImplementation} due to missing browser '
'features: ${result.missingFeatures}');
}
return result.resolvedExecutor;
}));
}
@DriftDatabase(tables: [
DriftActorTable,
], daos: [
DriftActorDao
])
class DriftDatabaseImpl extends _$DriftDatabaseImpl {
DriftDatabaseImpl._(super.e);
factory DriftDatabaseImpl() => DriftDatabaseImpl._(connectOnWeb());
@override
int get schemaVersion => 1;
}
class DriftActorEntity implements Insertable<DriftActorEntity> {
String? actorID;
String? parentActorID;
String? actorDescription;
String? actorDistributorId;
int? actorTypeID;
int? actorStatus;
int? actorMachinesCount;
DriftActorEntity(
{this.parentActorID,
this.actorID,
this.actorDescription,
this.actorDistributorId,
this.actorTypeID,
this.actorStatus,
this.actorMachinesCount});
@override
Map<String, Expression> toColumns(bool nullToAbsent) {
return DriftActorTableCompanion(
actorID: Value(actorID),
parentActorID: Value(parentActorID),
actorDescription: Value(actorDescription),
actorDistributorId: Value(actorDistributorId),
actorTypeID: Value(actorTypeID),
actorStatus: Value(actorStatus),
actorMachinesCount: Value(actorMachinesCount),
).toColumns(nullToAbsent);
}
@override
String toString() {
return 'DriftActorEntity{actorID: $actorID, parentActorID: $parentActorID, actorDescription: $actorDescription, actorDistributorId: $actorDistributorId, actorTypeID: $actorTypeID, actorStatus: $actorStatus, actorMachinesCount: $actorMachinesCount}';
}
}
@UseRowClass(DriftActorEntity)
class DriftActorTable extends Table {
@override
String get tableName => 'actor';
TextColumn get actorID => text().named("actorID").nullable()();
TextColumn get parentActorID => text().named("parentActorID").nullable()();
TextColumn get actorDescription => text().named("actorDescription").nullable()();
TextColumn get actorDistributorId => text().named("actorDistributorId").nullable()();
IntColumn get actorTypeID => integer().named("actorTypeID").nullable()();
IntColumn get actorStatus => integer().named("actorStatus").nullable()();
IntColumn get actorMachinesCount => integer().named("actorMachinesCount").nullable()();
@override
Set<Column>? get primaryKey => {actorID};
}
@DriftAccessor(tables: [DriftActorTable])
class DriftActorDao extends DatabaseAccessor<DriftDatabaseImpl>
with _$DriftActorDaoMixin {
DriftActorDao(DriftDatabaseImpl db) : super(db);
@override
Future<void> insertActors(List<DriftActorEntity> actors) {
return batch((batch) {
batch.insertAll(driftActorTable, actors,
mode: InsertMode.insertOrReplace);
});
}
@override
Future<List<DriftActorEntity>> getActors() {
return (select(driftActorTable)).get();
}
}
Steps to reproduce
flutter build web --wasm --no-strip-wasmdhttpd '--headers=Cross-Origin-Embedder-Policy=credentialless;Cross-Origin-Opener-Policy=same-origin' --path=build/web --port=8085Developer Tools > ConsoleExpected results
We see log "initialize drift database" end everything works fine.
Actual results
We see log "initialize drift database" and error:
Code sample
Code sample
main.dart
drift_database.dart
drift_actor_entity.dart
DriftActorTable
DriftActorDao
Flutter Doctor output
Flutter Doctor output
drift_worker.js