davidmartos96 / sqflite_sqlcipher

SQLite flutter plugin
BSD 2-Clause "Simplified" License
99 stars 44 forks source link

LateError LateInitializationError: Field 'db' has not been initialized. #44

Closed peterhijma closed 3 years ago

peterhijma commented 3 years ago

If a user starts the app and is unauthenticated, I would like to log the user out and remove their database. But since using the new version of flutter + dart which has null safety and such, I get this error:

LateError: LateInitializationError: Field 'db' has not been initialized.
  File "encrypted_moor.dart", in _SqfliteDelegate.close
  File "engines.dart", line 310, in DelegatedDatabase.close
  File "db_base.dart", line 128, in GeneratedDatabase.close
  File "<asynchronous suspension>"
  File "app_db.dart", line 101, in AppDB.removeDatabase
  File "<asynchronous suspension>"
  File "authentication_service.dart", line 75, in AuthenticationService.removeUserData
  File "<asynchronous suspension>"
  File "base_screen.dart", line 42, in BaseScreen.build.<fn>

Any clue?

This is no problem of course when the user is authenticated and uses the logout button. As in the meantime the database is always initialized. This is only a problem if the user is authenticated, closes the app, and then restarts it.

davidmartos96 commented 3 years ago

@phijma That would be code to fix in the moor repository, in particular the following file https://github.com/simolus3/moor/blob/master/extras/encryption/lib/encrypted_moor.dart

You can try opening a PR. Otherwise you can simply don't call close if the user is unauthenticated, because there is not database open for it.

peterhijma commented 3 years ago

Thanks @davidmartos96. I saw encrypted_moor and somehow thought this was where I needed to be. Sorry to bother you!