TodoList, a beautiful open source project, provides an example of the simplest way to turn a local storage project into a cloud-synced project.

This App original address at https://github.com/asjqkkkk/flutter-todos. Why did you choose this project? Because the project itself uses local SQLite for data storage, it’s easy to convert it to an online PostGRE database for cloud synchronization.

Projects that don’t use SQLite can also be modified, but the storage logic needs to be rewritten. If a project has features that you particularly like and need, it’s worth the time.

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

Dart file in the project, and the next major task is to transform the local database functionality.

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

First we introduce the PostGRE driver for Dart and initialize the database.

import 'package:postgres/postgres.dart'; . connection = PostgreSQLConnection(dbIp, dbPort, dbName, username: dbAccount, password: dbPasswd);awaitconnection.open(); .Copy the code

Here you need to prepare a free cloud database MemFireDB with public IP, one-click creation, one-click query, very convenient

Here are some invitation codes for you to test.

BeAwWu, BeBJdb, BeAuWn, BeB0d9, Be8JJP, BeBSua

Then change the database creation mode to postGRE mode.

await connection.execute(""" CREATE TABLE todolist ( id SERIAL PRIMARY KEY, account TEXT, taskName TEXT, taskType TEXT, taskStatus INTEGER, taskDetailNum INTEGER, uniqueId TEXT, needUpdateToCloud TEXT, overallProgress TEXT, changeTimes INTEGER, createDate TEXT, finishDate TEXT, startDate TEXT, deadLine TEXT, detailList TEXT, taskIconBean TEXT, textColor TEXT, backgroundUrl TEXT); "" ");
Copy the code

Once that was done, we moved the database to the cloud. Next, change its business logic for all operations on the database to PostGRE mode.

You can see that there are several methods in the source code.

createTask
getTasks
updateTask
deleteTask
updateTasks
createTasks
getTaskByUniqueId
queryTask
Copy the code

If you look at one of the ways to add Todo, you can see that dart’s SQLite library encapsulates some of the SQL statements and allows you to insert a map directly, making it easier to use.

Future createTask(TaskBean task) async{...var sql = getInsertSql('todolist', task.toMap());
  await db.execute(sql, substitutionValues: task.toMap());
}
Copy the code

But the PostGRE library doesn’t provide that, so you need to do a bit of wrapping yourself.

Here are my encapsulated methods for inserting and updating 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(a); insert.write('INSERT');
  insert.write(' INTO ');
  insert.write(_escapeName(table));
  insert.write('(');

  finalsize = (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 nameinsert.write(_escapeName(colName)); sbValues.write(PostgreSQLFormat.id(colName)); }}); insert.write(sbValues); } insert.write(') ');

  var sql = insert.toString();
  return sql;
}

Copy the code

There are a couple of other packages that you’ll see in your project, and you’ll see the full code address at the end.

So the way we add notes here becomes.

Future createTask(TaskBean task) async{...var sql = getInsertSql('todolist', task.toMap());
  await db.execute(sql, substitutionValues: task.toMap());
}
Copy the code

The same other methods are also to be transformed, the transformation of these methods are completed.

If your application needs to be made available to others, write a database configuration interface to facilitate dynamic configuration of the database.

I wrote one here for your reference.

Here, this is done! 🐶

The App is still pretty good, but with cloud syncing, it’s even more powerful.

Here is just to give you an idea, more play you play your imagination.

Complete code (github.com/aliyoge/flu…)