drift icon indicating copy to clipboard operation
drift copied to clipboard

NoSuchMethodError: The method 'open' was called on null

Open ThangNguyen91 opened this issue 4 years ago • 16 comments

I got this error on some devices android and ios. NoSuchMethodError: The method 'open' was called on null. Receiver: null Tried calling: open("/data/data/co.mindset/app_flutter/db26.sqlite")

This is pubspec.yaml image image This is class Database image

ThangNguyen91 avatar Feb 22 '21 03:02 ThangNguyen91

Does it happen on all devices? All the time or just sporadic? I think this can only happen if there's an issue opening libsqlite3.so (or sqlite3.dylib on iOS), did you get any exception before that?

simolus3 avatar Feb 23 '21 21:02 simolus3

Does it happen on all devices? All the time or just sporadic? I think this can only happen if there's an issue opening libsqlite3.so (or sqlite3.dylib on iOS), did you get any exception before that?

Some devices have this error, I have log error and have found that certain cannot be initialized with some other errors. My users have encountered this error, I have tested on oppo a37 device and got the same error. SM-G960N,Samsung S9,Samsung S8 works for me.

Below some bugs I have encountered: image

ThangNguyen91 avatar Feb 24 '21 01:02 ThangNguyen91

Can you try adding this to your pubspec? It won't fix the problem, but it should hopefully give us a better stacktrace:

dependency_overrides:
  sqlite3:
    git:
      url: https://github.com/simolus3/sqlite3.dart.git
      ref: 19d9cad2db45092d342e45ad8bca11db271c4d11
      path: sqlite3/

Also, are you building appbundles? Can you try setting android.bundle.enableUncompressedNativeLibs=false in your gradle.properties if you are? Can you check the Android versions where this is happening? If it's Android 6 and below, can you try this workaround? You'd have to await the future before using moor (wrapping it in a LazyDatabase should work).

simolus3 avatar Feb 26 '21 14:02 simolus3

Thanks you for response, i'll try it. Currently I have wrapped it in LazyDatabase I sent the image earlier.

ThangNguyen91 avatar Mar 01 '21 01:03 ThangNguyen91

Hi, I have the same trouble with Android Emulator with Android version 8

/flutter ( 6678): [ERROR:flutter/lib/ui/ui_dart_state.cc(186)] Unhandled Exception: NoSuchMethodError: The method 'open' was called on null. E/flutter ( 6678): Receiver: null E/flutter ( 6678): Tried calling: open("/data/user/0/com.example.gestor/app_flutter/dbapp.sqlite") E/flutter ( 6678): #0 Object.noSuchMethod (dart:core-patch/object_patch.dart:54:5) E/flutter ( 6678): #1 _VmDelegate.open package:moor/…/ffi/vm_database.dart:126 E/flutter ( 6678): #2 DelegatedDatabase.ensureOpen. package:moor/…/helpers/engines.dart:255 E/flutter ( 6678): E/flutter ( 6678): #3 BasicLock.synchronized package:synchronized/src/basic_lock.dart:34 E/flutter ( 6678): E/flutter ( 6678): #4 QueryEngine.doWhenOpened. (package:moor/src/runtime/api/query_engine.dart) package:moor/…/api/query_engine.dart:1 E/flutter ( 6678): E/flutter ( 6678):

afonseca69 avatar Mar 10 '21 15:03 afonseca69

My pubspec

environment: sdk: ">=2.7.0 <3.0.0"

dependencies: font_awesome_flutter: ^8.11.0 moor: ^3.4.0 path_provider: ^1.6.24 path: ^1.7.0 mockito: ^4.1.3 dio: ^3.0.10 flutter_mobx: ^1.1.0+2 flutter_slidable: ^0.5.7 mobx: ^1.2.1+4 flutter_modular: ^2.0.1 flutter: sdk: flutter

dev_dependencies: moor_generator: ^3.4.0 modular_codegen: ^2.0.1 mobx_codegen: ^1.1.2 build_runner: flutter_test: sdk: flutter

afonseca69 avatar Mar 10 '21 15:03 afonseca69

Running on windows gives the same error

[ERROR:flutter/lib/ui/ui_dart_state.cc(186)] Unhandled Exception: NoSuchMethodError: The method 'open' was called on null. Receiver: null Tried calling: open("C:\Users\Andre\Documents\dbapp.sqlite") #0 Object.noSuchMethod (dart:core-patch/object_patch.dart:54:5) #1 _VmDelegate.open package:moor/…/ffi/vm_database.dart:126 #2 DelegatedDatabase.ensureOpen. package:moor/…/helpers/engines.dart:255 #3 BasicLock.synchronized package:synchronized/src/basic_lock.dart:34 #4 QueryEngine.doWhenOpened. (package:moor/src/runtime/api/query_engine.dart) package:moor/…/api/query_engine.dart:1

afonseca69 avatar Mar 10 '21 15:03 afonseca69

@afonseca69 Can you try to add the dependency override I suggested here as well (https://github.com/simolus3/moor/issues/1061#issuecomment-786693753)?

The error on Windows might be expected if you're not providing a custom strategy to open sqlite3 - this is documented.

simolus3 avatar Mar 10 '21 16:03 simolus3

@afonseca69 Can you try to add the dependency override I suggested here as well (#1061 (comment))?

After dependency override

E/flutter ( 7238): [ERROR:flutter/lib/ui/ui_dart_state.cc(186)] Unhandled Exception: Invalid argument(s): Failed to load dynamic library (dlopen failed: library "/data/data/com.example.gestor/lib/libsqlite3.so" not found) E/flutter ( 7238): #0 _open (dart:ffi-patch/ffi_dynamic_library_patch.dart:11:55) E/flutter ( 7238): #1 new DynamicLibrary.open (dart:ffi-patch/ffi_dynamic_library_patch.dart:20:12) E/flutter ( 7238): #2 _defaultOpen package:sqlite3/src/load_library.dart:40 E/flutter ( 7238): #3 OpenDynamicLibrary.openSqlite package:sqlite3/src/load_library.dart:110 E/flutter ( 7238): #4 sqlite3 package:sqlite3/…/api/sqlite3.dart:12 E/flutter ( 7238): #5 _VmDelegate.open package:moor/…/ffi/vm_database.dart:126 E/flutter ( 7238): #6 DelegatedDatabase.ensureOpen. package:moor/…/helpers/engines.dart:255 E/flutter ( 7238): E/flutter ( 7238): #7 BasicLock.synchronized package:synchronized/src/basic_lock.dart:34 E/flutter ( 7238): E/flutter ( 7238): #8 QueryEngine.doWhenOpened. (package:moor/src/runtime/api/query_engine.dart) package:moor/…/api/query_engine.dart:1 E/flutter ( 7238): E/flutter ( 7238):

afonseca69 avatar Mar 10 '21 17:03 afonseca69

On windows I have downloaded sqlite3.dll, put it at the directory and it is ok.

afonseca69 avatar Mar 10 '21 18:03 afonseca69

Invalid argument(s): Failed to load dynamic library

That's what I expected. But wait, you don't have a dependency on sqlite3_flutter_libs at all! That is required to use moor/ffi on Android/iOS. Can you add this to your pubspec?

dependencies:
  sqlite3_flutter_libs: ^0.4.1

simolus3 avatar Mar 10 '21 18:03 simolus3

Invalid argument(s): Failed to load dynamic library

That's what I expected. But wait, you don't have a dependency on sqlite3_flutter_libs at all! That is required to use moor/ffi on Android/iOS. Can you add this to your pubspec?

dependencies:
  sqlite3_flutter_libs: ^0.4.1

On https://pub.dev/packages/moor/install the only dependency it shows is moor

  1. Depend on it Add this to your package's pubspec.yaml file:

dependencies: moor: ^4.1.0

And the dependencies on the right side is

Dependencies collection, convert, meta, pedantic, sqlite3, synchronized

Now I added sqlite3_flutter_libs: ^0.4.1 and it is fine

Thank you

afonseca69 avatar Mar 10 '21 18:03 afonseca69

On https://pub.dev/packages/moor/install the only dependency it shows is moor

Yes, unfortunately we don't have control over that tab (https://github.com/dart-lang/pub-dev/issues/2967), which can make it confusing for some packages. The setup instructions for moor can be found here.

simolus3 avatar Mar 10 '21 21:03 simolus3

Hey, I'm using sqlite3_flutter_libs: ^ 0.4.0 + 1, and I can't dependency_overrides beacause like the image I posted, and I don't want to upgrade it to flutter 2.0.0 at the present time. I also see a fix for the sqlite3_flutter_libs 0.3.0 version here (https://github.com/simolus3/moor/issues/895) Do I need to dependency_overrides? image

ThangNguyen91 avatar Mar 11 '21 06:03 ThangNguyen91

You might be able to add another dependency override for meta: ^1.3.0.

But realistically we already know why this error happens: We can't DynamicLibrary.open('libsqlite3.so') for some reason. So I think we should try to figure out why that happens first. If it doesn't affect a wide range of devices, can you post on which devices you get this error? Are you using app bundles on Android?

simolus3 avatar Mar 11 '21 09:03 simolus3

@simolus3 i spend more than one week to solve this issue , I used all possible solved in github/moor and read all previous answer but nothing change, every time the error appear is The method 'open' was called on null. i tested in (pixel xl with android version 11, samsung note 8 with android version 9). also I used the final version of flutter and moor database and also sqlite3_flutter_libs.

  • i think there are some error when use moor with android os should look again when open connection with sqlite

finally , i used moor_flutter and remove moor and change open connection in database to FlutterQueryExecutor.inDatabaseFolder(path: 'db.sqlite', logStatements: false) and it works

TaifRaoof avatar Mar 27 '21 12:03 TaifRaoof