Skip to content

Condition inside a Transaction block that can raise "Bad state: This database has already been closed" Error #1370

Answered by simolus3
aldycool asked this question in Q&A
Discussion options

You must be logged in to vote

Essentially, this is a misuse of the transaction api. Moor won't be able to fix this, but we should at least report a much better error message in this case.

As you mentioned, using an async callback in forEach will schedule async work without awaiting it. The transaction will be closed after call to forEach, but before all of the insert calls are completed.
The workaround in this case is to use await Future.wait(someCollection.map((element) async {})), which will wait for everything to complete.

To avoid this confusion, moor should add checks throwing an error if a completed transaction is still being used.

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@simolus3
Comment options

@aldycool
Comment options

Answer selected by aldycool
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants