davidmartos96 / sqflite_sqlcipher

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

StateError - Bad state: No element #53

Closed peterhijma closed 2 years ago

peterhijma commented 2 years ago

I have some code where I want to wait for a database to open, before closing it. In this case this is happening when starting the app and the user is not authenticated.

await doWhenOpened((_) => attachedDatabase.close());

But since some time this results in an error:

StateError
Bad state: No element

Crashed in non-app: list.dart in ListMixin.single
encrypted_moor.dart in _SqfliteVersionDelegate.schemaVersion at line 89 within encrypted_moor
Called from: <asynchronous suspension>
engines.dart in DelegatedDatabase._runMigrations at line 307 within moor
Called from: <asynchronous suspension>
engines.dart in DelegatedDatabase.ensureOpen.<fn> at line 289 within moor

Somehow, result.single throws the StateError because result is empty, inside:

@override
  Future<int> get schemaVersion async {
    final result = await _db.rawQuery('PRAGMA user_version;');
    return result.single.values.first as int;
  }

I'm not sure yet how and why. (And if it's my own fault).

Any idea what I can do to prevent this error?

davidmartos96 commented 2 years ago

Have you tried with the moor_flutter (the non encrypted version)? It may be something related to how you are opening the database, and not the encryption library itself. If you are able to create a minimal example app that reproduces the error it would be great. I'm not sure how the user_version query wouldn't return any rows.

peterhijma commented 2 years ago

@davidmartos96 I'll try to reproduce later this week. So far only saw this error/exception come into Sentry from production... :)

davidmartos96 commented 2 years ago

@davidmartos96 I'll try to reproduce later this week. So far only saw this error/exception come into Sentry from production... :)

Did you end up figuring out your issue?

peterhijma commented 2 years ago

@davidmartos96 I'll try to reproduce later this week. So far only saw this error/exception come into Sentry from production... :)

Did you end up figuring out your issue?

I’ve not seen the error since! So for now I’ll close it. If I have updates in the future I’ll let you know.