diff --git a/sqflite_common_ffi/README.md b/sqflite_common_ffi/README.md index b50592ab..f9d14c64 100644 --- a/sqflite_common_ffi/README.md +++ b/sqflite_common_ffi/README.md @@ -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). diff --git a/sqflite_test_app/lib/main_ffi_async.dart b/sqflite_test_app/lib/main_ffi_async.dart new file mode 100644 index 00000000..cc375d6b --- /dev/null +++ b/sqflite_test_app/lib/main_ffi_async.dart @@ -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 main() async { + await mainFfiAsync(); +} + +/// Run using ffi (io or web) +Future 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 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 runFfiAsync() async { + mainExampleApp(); +} diff --git a/sqflite_test_app/pubspec.yaml b/sqflite_test_app/pubspec.yaml index 3c8d72d7..fe1eaec4 100644 --- a/sqflite_test_app/pubspec.yaml +++ b/sqflite_test_app/pubspec.yaml @@ -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: