Flutter App: Synchronize local Sembast DB with a Cloud DB

k8scale.io
3 min readOct 25, 2020

In this article we will be talking about how to sync your on device Sembast DB to a DB running in cloud.

There are many use-cases where you would want to store the data locally on the device first. For example: Restaurant POS app, Retail Store POS app, Warehouse inventory management app. Basically any use-case where you don’t want the operation to be blocked because of network availability.

Here we will be talk about using Sembast DB which is used by many Apps written in flutter. It’s a no-sql DB completely written in dart programming language. So its compatible with Android, iOS and other platforms.

We will be using Coral Gateway as a gateway server to relay the synchronization to a DB running in cloud. Coral Gateway server is similar to other database Gatway servers but it provides you the flexibility to change your backend to either MongoDB, Google Firestore or AWS DynamoDB.

There are many ways of achieving the above goal we will talk about one of the ways we have done it for one of the customer.

In this example we are taking a ItemStore which stores items information in for a grocery store app. The complete code can be found in github repo

We are using a persistence Queue using Sembast DB to save the updates. Later these updates are periodically sent to the Coral Gateway server which persists it into the DB of your choice.

Components in Flutter App to synchronize the data

QueueCommand is a object added to a Queue whenever an item is saved or updated. There is a Poller job running which fetch these items from the queue and sends them to server.

Coral Gateway client is a simple client which runs on top of websocket with protocols to send the sync data.

Coral Gateway server for this example is using Pebble DB to store the data. You can run the server on your local machine following the Readme file

Let us know if this was helpful for your next Flutter app project.

We will be publishing a next blog on what were the motivation behind building our own gateway server, instead of going with Google Firestore.

Checkout the next blog here:

If you like this article follow us on Twitter: https://twitter.com/k8scaleio

--

--