pinchbv / floor

The typesafe, reactive, and lightweight SQLite abstraction for your Flutter applications
https://pinchbv.github.io/floor/
Apache License 2.0
976 stars 191 forks source link

Unhandled Exception: DatabaseException(error database_closed) #745

Open lumingyuan opened 1 year ago

lumingyuan commented 1 year ago

How can I solve the issue where a StreamBuilder refreshes and throws an exception when I try to close the database?

E/flutter ( 4865): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: DatabaseException(error database_closed) E/flutter ( 4865): #0 SqfliteDatabaseMixin.checkNotClosed package:sqflite_common/src/database_mixin.dart:337 E/flutter ( 4865): #1 SqfliteDatabaseExecutorMixin._rawQuery package:sqflite_common/src/database_mixin.dart:124 E/flutter ( 4865): #2 SqfliteDatabaseExecutorMixin.query package:sqflite_common/src/database_mixin.dart:10

lumingyuan commented 1 year ago

My requirement is to restore data, so I need to close the database first, then overwrite the database file and reopen it. However, when I use dao.queryStream with StreamBuilder, closing the database will prompt that the database has already been closed. How can I write this logic correctly?

SEGVeenstra commented 1 year ago

I think you should not directly rely on the Stream in the StreamBuilder. If you use a StreamBuilder directly, I think it will expect to be responsible for the closing, so when you close it yourself, it might continue tryin to read a closed stream.

I think you will have to manage this yourself, depending on your app's architecture, a StatefulWidget might do.