Skip to content

Commit

Permalink
[feat] start sqflite_common_ffi_async based on sqlite_async
Browse files Browse the repository at this point in the history
  • Loading branch information
alextekartik committed Dec 15, 2023
1 parent e6286a1 commit 4252869
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 4 deletions.
6 changes: 2 additions & 4 deletions sqflite_common_ffi/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@
[sqflite](https://pub.dev/packages/sqflite) based ffi implementation. Based
on [`sqlite3`](https://pub.dev/packages/sqlite3). Thanks to [Simon Binder](https://github.com/simolus3)

Currently supported on Linux, MacOS and Windows on both Flutter and Dart VM.

While not tested extensively, it also
works on iOS and Android (using [sqlite3_flutter_libs](https://pub.dev/packages/sqlite3_flutter_libs) - Thanks
* Works on Linux, MacOS and Windows on both Flutter and Dart VM.
* Works on iOS and Android (using [sqlite3_flutter_libs](https://pub.dev/packages/sqlite3_flutter_libs) - Thanks
to [Simon Binder](https://github.com/simolus3))

It allows also mocking sqflite during regular flutter unit test (i.e. not using the emulator/simulator).
Expand Down
44 changes: 44 additions & 0 deletions sqflite_test_app/lib/main_ffi_async.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:sqflite/sqflite.dart';
import 'package:sqflite_common_ffi/sqflite_ffi.dart';
import 'package:sqflite_common_ffi_async/sqflite_ffi_async.dart';
import 'package:sqflite_example/main.dart';

Future<void> main() async {
await mainFfiAsync();
}

/// Run using ffi (io or web)
Future<void> mainFfiAsync() async {
await initFfiAsync();
await runFfiAsync();
}

/// Init Ffi for io or web
///
/// if [noWorker] is true, no isolate is used on io and no web worker is used on the web.
Future<void> initFfiAsync() async {
// getDatabasesPath implementation is lame, use the default one
// but we could also use path_provider
var isSqfliteCompatible =
(Platform.isAndroid || Platform.isIOS || Platform.isMacOS);
//DatabaseFactory? original;
// Save original for iOS & Android

sqfliteFfiInit();
databaseFactoryOrNull = databaseFactoryFfiAsync;

WidgetsFlutterBinding.ensureInitialized();
// Use sqflite databases path provider (ffi implementation is lame))
if (isSqfliteCompatible) {
await databaseFactory.setDatabasesPath(
await databaseFactorySqflitePlugin.getDatabasesPath());
}
}

/// Run example app.
Future<void> runFfiAsync() async {
mainExampleApp();
}
2 changes: 2 additions & 0 deletions sqflite_test_app/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ dependencies:
cupertino_icons: '>=0.1.3'
sqflite: '>=2.0.0-nullsafety.2'
sqflite_common_ffi:
sqflite_common_ffi_async:
path: ../packages/sqflite_common_ffi_async
sqflite_example:
path: ../sqflite/example
flutter_web_plugins:
Expand Down

0 comments on commit 4252869

Please sign in to comment.