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 https://github.com/bimsina/notes-app. 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 connection.open(); .

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.

getNoteMapList
insertNote
updateNote
deleteNote
getCount
getNoteList

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 = ignores.map((e) => 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(PostgreSQLFormat.id(colName)); }}); 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="https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/a4d3e09e7b6047dbb7a441b0cd227260~tplv-k3u1fbpfcp-zoom-1.image" width="400"/>

</center>

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 (https://github.com/aliyoge/no…