simolus3/sqlite3.dart

Unavailable library `dart:ffi` and missing type errors when trying to run on web

Closed this issue · 2 comments

This stackexchange post and simolus3/drift#2355 is probably related to this, but I do not know what to do with that info.

Trying to run this small pet project of mine throws the error below.

Steps to Reproduce

Error output

output-log.txt

Launching lib/main.dart on Chrome in debug mode...
Warning: In index.html:37: Local variable for "serviceWorkerVersion" is deprecated. Use "{{flutter_service_worker_version}}" template token instead.
Warning: In index.html:46: "FlutterLoader.loadEntrypoint" is deprecated. Use "FlutterLoader.load" instead.
/home/user/.pub-cache/hosted/pub.dev/sqlite3-2.4.2/lib/src/ffi/api.dart:1:8: Error: Dart library 'dart:ffi' is not available on this platform.
import 'dart:ffi';
       ^
Context: The unavailable library 'dart:ffi' is imported through these packages:

    web_entrypoint.dart => package:cohabit => package:sqlite3 => dart:ffi
    web_entrypoint.dart => package:cohabit => package:sqlite3 => package:ffi => dart:ffi

Detailed import paths for (some of) the these imports:

    web_entrypoint.dart => package:cohabit/main.dart => package:cohabit/landing_screen.dart => package:cohabit/home_page.dart => package:cohabit/nav_destinations.dart => package:cohabit/habit_list_page.dart =>
    package:sqlite3/sqlite3.dart => package:sqlite3/src/ffi/api.dart => dart:ffi
    web_entrypoint.dart => package:cohabit/main.dart => package:cohabit/landing_screen.dart => package:cohabit/home_page.dart => package:cohabit/nav_destinations.dart => package:cohabit/habit_list_page.dart =>
    package:sqlite3/sqlite3.dart => package:sqlite3/src/ffi/api.dart => package:sqlite3/open.dart => package:sqlite3/src/ffi/load_library.dart => dart:ffi
    web_entrypoint.dart => package:cohabit/main.dart => package:cohabit/landing_screen.dart => package:cohabit/home_page.dart => package:cohabit/nav_destinations.dart => package:cohabit/habit_list_page.dart =>
    package:sqlite3/sqlite3.dart => package:sqlite3/src/ffi/api.dart => package:sqlite3/open.dart => dart:ffi
    web_entrypoint.dart => package:cohabit/main.dart => package:cohabit/landing_screen.dart => package:cohabit/home_page.dart => package:cohabit/nav_destinations.dart => package:cohabit/habit_list_page.dart =>
    package:sqlite3/sqlite3.dart => package:sqlite3/src/ffi/api.dart => package:sqlite3/src/ffi/implementation.dart => dart:ffi
    web_entrypoint.dart => package:cohabit/main.dart => package:cohabit/landing_screen.dart => package:cohabit/home_page.dart => package:cohabit/nav_destinations.dart => package:cohabit/habit_list_page.dart =>
    package:sqlite3/sqlite3.dart => package:sqlite3/src/ffi/api.dart => package:sqlite3/src/ffi/implementation.dart => package:sqlite3/src/ffi/bindings.dart => dart:ffi
    web_entrypoint.dart => package:cohabit/main.dart => package:cohabit/landing_screen.dart => package:cohabit/home_page.dart => package:cohabit/nav_destinations.dart => package:cohabit/habit_list_page.dart =>
    package:sqlite3/sqlite3.dart => package:sqlite3/src/ffi/api.dart => package:sqlite3/src/ffi/implementation.dart => package:sqlite3/src/ffi/bindings.dart => package:sqlite3/src/ffi/memory.dart => dart:ffi
    web_entrypoint.dart => package:cohabit/main.dart => package:cohabit/landing_screen.dart => package:cohabit/home_page.dart => package:cohabit/nav_destinations.dart => package:cohabit/habit_list_page.dart =>
    package:sqlite3/sqlite3.dart => package:sqlite3/src/ffi/api.dart => package:sqlite3/src/ffi/implementation.dart => package:sqlite3/src/ffi/bindings.dart => package:sqlite3/src/ffi/memory.dart => package:ffi/ffi.dart =>
    package:ffi/src/allocation.dart => dart:ffi
    web_entrypoint.dart => package:cohabit/main.dart => package:cohabit/landing_screen.dart => package:cohabit/home_page.dart => package:cohabit/nav_destinations.dart => package:cohabit/habit_list_page.dart =>
    package:sqlite3/sqlite3.dart => package:sqlite3/src/ffi/api.dart => package:sqlite3/src/ffi/implementation.dart => package:sqlite3/src/ffi/bindings.dart => package:sqlite3/src/ffi/memory.dart => package:ffi/ffi.dart =>
    package:ffi/src/arena.dart => dart:ffi
    web_entrypoint.dart => package:cohabit/main.dart => package:cohabit/landing_screen.dart => package:cohabit/home_page.dart => package:cohabit/nav_destinations.dart => package:cohabit/habit_list_page.dart =>
    package:sqlite3/sqlite3.dart => package:sqlite3/src/ffi/api.dart => package:sqlite3/src/ffi/implementation.dart => package:sqlite3/src/ffi/bindings.dart => package:sqlite3/src/ffi/memory.dart => package:ffi/ffi.dart =>
    package:ffi/src/utf8.dart => dart:ffi
    web_entrypoint.dart => package:cohabit/main.dart => package:cohabit/landing_screen.dart => package:cohabit/home_page.dart => package:cohabit/nav_destinations.dart => package:cohabit/habit_list_page.dart =>
    package:sqlite3/sqlite3.dart => package:sqlite3/src/ffi/api.dart => package:sqlite3/src/ffi/implementation.dart => package:sqlite3/src/ffi/bindings.dart => package:sqlite3/src/ffi/memory.dart => package:ffi/ffi.dart =>
    package:ffi/src/utf16.dart => dart:ffi
    web_entrypoint.dart => package:cohabit/main.dart => package:cohabit/landing_screen.dart => package:cohabit/home_page.dart => package:cohabit/nav_destinations.dart => package:cohabit/habit_list_page.dart =>
    package:sqlite3/sqlite3.dart => package:sqlite3/src/ffi/api.dart => package:sqlite3/src/ffi/implementation.dart => package:sqlite3/src/ffi/bindings.dart => package:sqlite3/src/ffi/memory.dart => package:sqlite3/src/ffi/sqlite3.g.dart
    => dart:ffi
    web_entrypoint.dart => package:cohabit/main.dart => package:cohabit/landing_screen.dart => package:cohabit/home_page.dart => package:cohabit/nav_destinations.dart => package:cohabit/habit_list_page.dart =>
    package:sqlite3/sqlite3.dart => package:sqlite3/src/ffi/api.dart => package:sqlite3/src/ffi/implementation.dart => package:sqlite3/src/ffi/bindings.dart => package:sqlite3/src/ffi/sqlite3.g.dart => dart:ffi
    web_entrypoint.dart => package:cohabit/main.dart => package:cohabit/landing_screen.dart => package:cohabit/home_page.dart => package:cohabit/nav_destinations.dart => package:cohabit/habit_list_page.dart =>
    package:sqlite3/sqlite3.dart => package:sqlite3/src/ffi/api.dart => package:sqlite3/src/ffi/implementation.dart => package:sqlite3/src/ffi/memory.dart => dart:ffi
    web_entrypoint.dart => package:cohabit/main.dart => package:cohabit/landing_screen.dart => package:cohabit/home_page.dart => package:cohabit/nav_destinations.dart => package:cohabit/habit_list_page.dart =>
    package:sqlite3/sqlite3.dart => package:sqlite3/src/ffi/api.dart => package:sqlite3/src/ffi/implementation.dart => package:sqlite3/src/ffi/memory.dart => package:ffi/ffi.dart => package:ffi/src/allocation.dart => dart:ffi
    web_entrypoint.dart => package:cohabit/main.dart => package:cohabit/landing_screen.dart => package:cohabit/home_page.dart => package:cohabit/nav_destinations.dart => package:cohabit/habit_list_page.dart =>
    package:sqlite3/sqlite3.dart => package:sqlite3/src/ffi/api.dart => package:sqlite3/src/ffi/implementation.dart => package:sqlite3/src/ffi/memory.dart => package:ffi/ffi.dart => package:ffi/src/arena.dart => dart:ffi
    web_entrypoint.dart => package:cohabit/main.dart => package:cohabit/landing_screen.dart => package:cohabit/home_page.dart => package:cohabit/nav_destinations.dart => package:cohabit/habit_list_page.dart =>
    package:sqlite3/sqlite3.dart => package:sqlite3/src/ffi/api.dart => package:sqlite3/src/ffi/implementation.dart => package:sqlite3/src/ffi/memory.dart => package:ffi/ffi.dart => package:ffi/src/utf8.dart => dart:ffi
    web_entrypoint.dart => package:cohabit/main.dart => package:cohabit/landing_screen.dart => package:cohabit/home_page.dart => package:cohabit/nav_destinations.dart => package:cohabit/habit_list_page.dart =>
    package:sqlite3/sqlite3.dart => package:sqlite3/src/ffi/api.dart => package:sqlite3/src/ffi/implementation.dart => package:sqlite3/src/ffi/memory.dart => package:ffi/ffi.dart => package:ffi/src/utf16.dart => dart:ffi
    web_entrypoint.dart => package:cohabit/main.dart => package:cohabit/landing_screen.dart => package:cohabit/home_page.dart => package:cohabit/nav_destinations.dart => package:cohabit/habit_list_page.dart =>
    package:sqlite3/sqlite3.dart => package:sqlite3/src/ffi/api.dart => package:sqlite3/src/ffi/implementation.dart => package:sqlite3/src/ffi/memory.dart => package:sqlite3/src/ffi/sqlite3.g.dart => dart:ffi
    web_entrypoint.dart => package:cohabit/main.dart => package:cohabit/landing_screen.dart => package:cohabit/home_page.dart => package:cohabit/nav_destinations.dart => package:cohabit/habit_list_page.dart =>
    package:sqlite3/sqlite3.dart => package:sqlite3/src/ffi/api.dart => package:sqlite3/src/ffi/implementation.dart => package:sqlite3/src/ffi/sqlite3.g.dart => dart:ffi

/home/user/.pub-cache/hosted/pub.dev/sqlite3-2.4.2/lib/open.dart:5:8: Error: Dart library 'dart:ffi' is not available on this platform.
import 'dart:ffi';
       ^
/home/user/.pub-cache/hosted/pub.dev/sqlite3-2.4.2/lib/src/ffi/implementation.dart:1:8: Error: Dart library 'dart:ffi' is not available on this platform.
import 'dart:ffi';
       ^
/home/user/.pub-cache/hosted/pub.dev/sqlite3-2.4.2/lib/src/ffi/load_library.dart:1:8: Error: Dart library 'dart:ffi' is not available on this platform.
import 'dart:ffi';
       ^
/home/user/.pub-cache/hosted/pub.dev/sqlite3-2.4.2/lib/src/ffi/bindings.dart:3:8: Error: Dart library 'dart:ffi' is not available on this platform.
import 'dart:ffi';
       ^
/home/user/.pub-cache/hosted/pub.dev/sqlite3-2.4.2/lib/src/ffi/memory.dart:2:8: Error: Dart library 'dart:ffi' is not available on this platform.
import 'dart:ffi';
       ^
/home/user/.pub-cache/hosted/pub.dev/sqlite3-2.4.2/lib/src/ffi/sqlite3.g.dart:5:8: Error: Dart library 'dart:ffi' is not available on this platform.
import 'dart:ffi' as ffi;
       ^
/home/user/.pub-cache/hosted/pub.dev/ffi-2.1.2/lib/src/allocation.dart:5:8: Error: Dart library 'dart:ffi' is not available on this platform.
import 'dart:ffi';
       ^
/home/user/.pub-cache/hosted/pub.dev/ffi-2.1.2/lib/src/arena.dart:8:8: Error: Dart library 'dart:ffi' is not available on this platform.
import 'dart:ffi';
       ^
...
...
...
/home/user/.pub-cache/hosted/pub.dev/ffi-2.1.2/lib/src/utf16.dart:95:11: Error: 'Pointer' isn't a type.
    final Pointer<Uint16> result = allocator<Uint16>(units.length + 1);
          ^^^^^^^
/home/user/.pub-cache/hosted/pub.dev/ffi-2.1.2/lib/src/utf16.dart:95:46: Error: 'Uint16' isn't a type.
    final Pointer<Uint16> result = allocator<Uint16>(units.length + 1);
                                             ^^^^^^
Unhandled exception:
Unsupported operation: Unsupported invalid type InvalidType(<invalid>) (InvalidType). Encountered while compiling file:///home/user/.pub-cache/hosted/pub.dev/sqlite3-2.4.2/lib/src/ffi/implementation.dart, which contains the type:
FunctionType(FfiSqlite3 Function(<invalid>)).
#0      ProgramCompiler._typeCompilationError (package:dev_compiler/src/kernel/compiler.dart:3236)
#1      ProgramCompiler._emitType (package:dev_compiler/src/kernel/compiler.dart:3197)
#2      ProgramCompiler._emitClassSignature (package:dev_compiler/src/kernel/compiler.dart:1570)
#3      ProgramCompiler._emitClassDeclaration (package:dev_compiler/src/kernel/compiler.dart:962)
#4      ProgramCompiler._emitClass (package:dev_compiler/src/kernel/compiler.dart:819)
#5      List.forEach (dart:core-patch/growable_array.dart:416)
#6      ProgramCompiler._emitLibrary (package:dev_compiler/src/kernel/compiler.dart:757)
#7      List.forEach (dart:core-patch/growable_array.dart:416)
#8      ProgramCompiler.emitModule (package:dev_compiler/src/kernel/compiler.dart:486)
#9      IncrementalJavaScriptBundler.compile (package:frontend_server/src/javascript_bundle.dart:223)
#10     FrontendCompiler.writeJavaScriptBundle (package:frontend_server/frontend_server.dart:801)
<asynchronous suspension>
#11     FrontendCompiler.compile (package:frontend_server/frontend_server.dart:661)
<asynchronous suspension>
#12     listenAndCompile.<anonymous closure> (package:frontend_server/frontend_server.dart:1311)
<asynchronous suspension>
the Dart compiler exited unexpectedly.
Waiting for connection from debug service on Chrome...             12.9s
Failed to compile application.

The core package:sqlite3/sqlite3.dart import doesn't work on the web, and never will as it depends on dart:ffi. Getting sqlite3 to run on the web is pretty complex due to varying browser support, and none of that is implemented in the core sqlite3 package. I strongly suggest using a wrapper like drift or sqflite (with sqflite_common_ffi_web for the web) which takes care of this for you.

You can work around this problem by replacing most of your package:sqlite3/sqlite3.dart imports with package:sqlite3/common.dart, Database with CommonDatabase and so on. Then the only remaining challenge is how you open sqlite3 databases on the web, for which this section of the readme provides some ideas.

You can work around this problem by replacing most of your package:sqlite3/sqlite3.dart imports with package:sqlite3/common.dart, Database with CommonDatabase and so on. Then the only remaining challenge is how you open sqlite3 databases on the web, for which this section of the readme provides some ideas.

I see. It seems I accidentally imported package:sqlite3/sqlite3.dart somewhere in the code. Sorry for the noise and thank you for the time.