DEV Community

Taiwo Divine
Taiwo Divine

Posted on

How to bundle Sqflite_common_ffi Database file in Windows .exe executable: Flutter Offline Desktop App

Why I decided to write this post
I recently developed an inventory desktop application for a client using Flutter and online database. While the the app solved the client's need, the client soon became frustrated using the app due to low internet connection. I decided to introduce an offline version of the app because I like satisfying my clients. Whilst there are many options to choose from, I decided to settle with sqflite_common_ffi.

I read through lots of related posts and watched some YouTube videos, and whilst some of the post made me realized I could include my database file through flutter assets (links below), still none of them gave detailed guide to put me through how to bundle Sqflite_common_ffi Database file with .exe executable. I had to rely on my instinct and eventually I scaled through. Hence, the reason I decided to write this post to continue where others stopped.

What I hoped you already know
First, you should already know how to build Flutter desktop app in debug mode with backend capability before getting here. Second, in other not to re-invent the wheel, I will only mention and include links but won't repeat some steps which will be needed to complete this task.

Let's Get Started
Step 1:
Build Your Flutter Desktop App - https://docs.flutter.dev/get-started/install/windows/desktop

Step 2:
Add Database Capability - https://pub.dev/packages/sqflite_common_ffi

Step 3:
Include Database file inside asset folder - https://github.com/tekartik/sqflite/blob/master/sqflite/doc/opening_asset_db.md
Notes on step 3: I recommend you use the getDatabasesPath() method to define your database path as that will make it easier for all your files to be bundle together after your desktop app is installed. If you use getApplicationDocumentsPath, your database file will be in the installed system document's directory while your app executables will be somewhere else.

Step 4:

  1. Run "flutter build windows --release" and navigate to the folder where the release executables are located. most likely it will be in the following path "{path_to_application}\build\windows\x64\runner\Release".
  2. Launch the Executable in this "Release folder" first before proceeding to the next step. This action will create another folder with your database file in the "Release" directory. Note: If you don't launch this executable before proceeding to step 5, your application will build successfully but the database file will not be bundled. It took me hours to discover this.

Step 5
Package and Distribute your Flutter Offline Desktop App - https://medium.com/@fluttergems/packaging-and-distributing-flutter-desktop-apps-the-missing-guide-part-2-windows-0b468d5e9e70

Conclusion
Endeavor to follow the steps in each steps and you will safely build your Flutter Offline Windows App. Should you need any help on this, comment on this post and I'll be willing to help.

Top comments (0)