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

This App original 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.

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

Since the project used SQLite, we are going to change it to Postgres.

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 you need to prepare a public network IP database, recommend a convenient cloud database, one key to create, one key query, very convenient, and is free of charge Oh. Go to MemFireDB and register an account to use it.

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

await connection.execute(""" CREATE TABLE $noteTable ( $colId SERIAL PRIMARY KEY, $colTitle TEXT, $colDescription TEXT, $colPriority INTEGER, $colColor INTEGER, $colDate TEXT); "" ");

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 notes, we can see that Dart’s SQLite library does some wrapping for SQL statements, and can directly insert a map, which is easy to use.

Future<int> insertNote(Note note) async {
  Database db = await this.database;
  var result = await db.insert(noteTable, note.toMap());
  return result;

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 other encapsulations that you can look at in your project, and you’ll get the full code address at the end.

So the way we add notes here is going to be.

Future<int> insertNote(Note note) async {

  var sql = getInsertSql(noteTable, note.toMap());
  var res = await db.execute(sql, substitutionValues: note.toMap());
  return res;

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

If your app needs to be available to others, or you want to keep a diary with your object (if you have one) 🐶.

Then to write a configuration database interface, convenient dynamic configuration database.

I have one here for reference.

<center class=”half”>

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


Arrive here, you can write diary together happily with your object! 🐶

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 (…