How to quickly add cloud synchronization to a project

Today, a beautiful open source billing project is used as an example to give you an idea of the simplest way to turn a local storage project into a cloud-synchronized project.

This App is called “out”, address at Why did you choose this project? Because the project itself is using local SQLite for data storage, it is more convenient to change it into an online PostGRE database, so as to use cloud synchronization function.

For projects that are not using SQLite, it is possible to reinvent the storage logic. If there is a feature that you particularly like and need in a project, it is worth the time to reinvent.

Without further ado, let’s look at the project.

First we need to prepare a test phone, we use the latest Hongmeng system to test

We found the db_provider.dart file in our project, and the next major task is to transform the local database functionality.

First we introduce Dart’s PostGre driver and initialize the database.

import 'package:postgres/postgres.dart'; . connection = PostgreSQLConnection(dbIp, dbPort, dbName, username: dbAccount, password: dbPasswd); await; .

Here we need to prepare a free cloud database MemfireDB with public network IP, which is very convenient to create and query with one key.

The next step is to change the database creation method to PostGRE.

CREATE TABLE $tableName (""" $colId SERIAL PRIMARY KEY, $colUuid TEXT, $colTimestamp BIGINT, $colDescription TEXT, $colType INTEGER, $colAmount REAL); "" ");

Once this step was successful, we moved the database to the cloud. Next, change all of its business logic for manipulating the database to PostGre mode.

You can see several of these methods in the source code.


If we look at one of the ways to add transactions, we can see that Dart’s SQLite library does some wrapping around the SQL statement, and can directly insert a map, which is easy to use.

Future<int> addTransaction(Transaction transaction) async {

  return db.insert('Transactions', transaction.toMap());

But the PostGRE library doesn’t provide this, so you need to do some wrapping yourself. Here is the method I encapsulated to make it easy to insert and update data.

static String getInsertSql(String table, Map<String, dynamic> values, {List<String> ignores}) { if (ignores ! = null && ignores.length > 0) { ignores = => e.toLowerCase()).toList(); } final insert = StringBuffer(); insert.write('INSERT'); insert.write(' INTO '); insert.write(_escapeName(table)); insert.write(' ('); final size = (values ! = null) ? values.length : 0; if (size > 0) { final sbValues = StringBuffer(') VALUES ('); var i = 0; values.forEach((String colName, dynamic value) { if (ignores == null || ! ignores.contains(colName.toLowerCase())) { if (i++ > 0) { insert.write(', '); sbValues.write(', '); } /// This should be just a column name insert.write(_escapeName(colName)); sbValues.write(; }}); insert.write(sbValues); } insert.write(')'); var sql = insert.toString(); return sql; }

There are a few other wrappers, all written in the pg_helper.dart file.

So the way we’re going to add transactions here is going to be.

Future<int> addTransaction(Transaction transaction) async {

  var sql = PgHelper.getInsertSql(tableName, transaction.toMap(), ignores: [colId]);
  var res = await db.execute(sql, substitutionValues: transaction.toMap());
  return res;

Similarly, all the other methods have to be reformed, and when these methods are reformed, they are finished.

If your application needs to be available to others, or if you want to account with your objects (if you have one), you can write a configuration database interface that makes it easy to configure the database dynamically.

I have one here for reference.

<center class=”half”>

<img src="" width="400"/>


Arrive here, you can keep an account together with your object happily! [head]

The App is still very good, and now with cloud synchronization, it’s even more powerful.

Here is just to provide you with a train of thought, more play we play their imagination.

The complete code (…