I am developing a flutter app that should also be able to run in offline mode. Because I am using flutter I want to also offer the use of the web version of my application. The application I want to build is data reliant therefore to make it work offline I need to have some kind of local database, but for the web version to work, I also need to store the data on a remote database so it can be accessed from the web. The problem that this proposes is how do I makes sure that the local and remote databases are always on the same page. If something is changed from the web it needs to also affect the local database on the device and if something is changed locally it also has to affect the remote database. I simply need a tip on how to generally achieve what I am looking for.
-
I personally prefer to use Firebase in this situation. It has support for offline access and automatic syncs. However, I'm not sure how one would go about implementing it from scratch. I'd like to learn this too. – Hussain Aug 14 '21 at 14:23
1 Answers
This can be easily achieved using Firebase Firestore
which supports offline.
Or
If you plan to do this from scratch one way to do this is to keep a separate local database and execute every transaction on it. If online do the same transactions on the remote database. If offline keep a record of transactions pending on the local database ( preferably in a separate DB from the main DB) and execute them when the remote database is connected.
You can use Hive
or sqflite
for local DB
Or
Keep track of records, which the transactions were performed on. Before synchronize merge these records from both local(done offline) and remote(done on the web, phone not connected). If multiple transactions were performed on the same record, Update both remote & local DB records to the latest record state from DB where the latest transaction was performed (if the latest transaction was performed on the remote DB, update the local DB& vice-versa)

- 2,140
- 1
- 14
- 19
-
What if the user makes some transactions to the remote database from the web how do I get them onto the local database. Does the user's device have to be connected to the internet while using the web version like you have to when using whatsapp web. Maybe you could also store transactions made at the remote database from the web and later synchronize them with the local database, but what if the user's device is not connected to the internet and he makes changes on the local database and changes on the remote database from the web. I think that would cause problems. – TheUltimateOptimist Aug 14 '21 at 18:35
-
I updated my answer and added a new way. Hope that solves the conflict issues. – NirmalCode Aug 14 '21 at 19:40