How to sync a mobile app offline state with a remote database?

Viewed 1011

I am building a mobile app using Flutter. All user's data is stored online in a MySQL database, so the app needs an internet connection for almost every user interaction (there is a backend REST API).

Users have to be able to create some lists of tasks, update and delete every task and list, and so on. But from the user's perspective, the need for an internet connection for every simple operation like adding or deleting a task is a bad experience. I need a way to support these operations even without connection with the backend and to apply these changes later when it is possible. But what is the best practice to handle this case?

How to keep the app behaving like normal even without an internet connection and sync all changes that the user has done with the backend when the internet is available again?

For example, if the user creates a new list the app expects to receive the new list's object (with id) from the backend. Later this id is used for every backend call about this list like adding a task in it.

1 Answers

What you can do is use a state management approaches like Providers, Bloc etc and have a local state of your database or the needed list inside them and apply all the changes on them when offline and implement all these on to the server when connected to internet.

Read here about flutter state Management

also you can check when the device is connected to internet with this connectivity and data_connection_checker packages

Related