What's the best practice to close the Store instance in a Flutter app?


I am building a Flutter app with ObjectBox database.

App calls openStore(); at launch, and saves the Store instance in a singleton class so that I can access to it from anywhere.

But I’m not sure when to call store.close();

In iOS we can execute code right before app termination using applicationWillTerminate(_:)

Unfortunately, Flutter does not provide us something like this.

I tried below code in the longest live expected StatefulWidget but it sometimes dispose sooner and the app loses access to the database.

void dispose() {

My questions are…

  1. What’s the best practice to call store.close()?
  2. What happen if I don’t close the store? Would my app lose data persistency?


What you’re doing seems OK to me. You could change it up slightly to StoreHelper().close() which would also clear set its Store reference to null, in which case if some other part of the app still tried to access, you could handle that (recognize it’s closed or reopen it).

Also, in most cases, you should get away without closing the store explicitly. Unless you’re doing (many) background write operations in which case you should probably try to at least wait for it to finish in dispose().

Answered By – vaind

Answer Checked By – Robin (FlutterFixes Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *