Flutter
We can use LiteSync on Flutter apps on both Android and iOS using the litesync_sqflite plugin
Instructions
Add the dependency to your flutter project:
dependencies: ... litesync_sqflite:
Then run:
flutter pub get
To install the free version of LiteSync native libraries, execute the following:
export FLUTTER_PATH=/path/to/flutter/sdk # <-- put the path to the Flutter SDK here echo '----- Android -----' wget http://litesync.io/download/litesync.aar mv litesync.aar $FLUTTER_PATH/.pub-cache/hosted/pub.dartlang.org/litesync_sqflite-*/android/ echo '----- iOS -----' mkdir litesync && cd litesync wget http://litesync.io/download/litesync-free-ios-native-libs.tar.gz tar zxvf litesync-free-ios-native-libs.tar.gz cd ..
When moving to the full version just copy the libraries to the respective folders as done above, replacing the existing files.
Example Code
import 'package:litesync_sqflite/sqflite.dart';
// Get a location using getDatabasesPath
var databasesPath = await getDatabasesPath();
String path = join(databasesPath, 'app.db');
String uri = 'file:' + path + '?node=secondary&connect=tcp://111.222.33.44:1234';
// open the database
Database db = await openDatabase(uri);
db.events(onNotReady: () {
// the database is not ready yet. show some waiting screen
...
}, onReady: () {
// the database is ready to be accessed. show the main screen
...
}, onSync: () {
// the database received an update. update the screen with new data
...
});
// It is also possible to check without events:
if await db.isReady() {
...
}
// To check the full status of the database we can use:
var res = await db.rawQuery('PRAGMA sync_status');
print('sync status: ${res}');
// --- the code below should be executed only after the database is ready for access ---
// Insert some records in a transaction
await db.transaction((txn) async {
int id1 = await txn.rawInsert('INSERT INTO Test(name, value, num) VALUES("some name", 1234, 456.789)');
int id2 = await txn.rawInsert(
'INSERT INTO Test(name, value, num) VALUES(?, ?, ?)',
['another name', 12345678, 3.1416]);
});
// Update some record
int count = await db.rawUpdate(
'UPDATE Test SET name = ?, value = ? WHERE name = ?',
['updated name', '9876', 'some name']);
// Read the records
List
Sample Project
Here is a sample Flutter application that uses LiteSync:
Download it and extract the files to an empty folder
Then copy and paste these commands in the terminal, inside the created folder:
flutter pub get export FLUTTER_PATH=/path/to/flutter/sdk # <-- put the path to the Flutter SDK here echo '----- Android -----' wget http://litesync.io/download/litesync.aar mv litesync.aar $FLUTTER_PATH/.pub-cache/hosted/pub.dartlang.org/litesync_sqflite-*/android/ echo '----- iOS -----' mkdir litesync && cd litesync wget http://litesync.io/download/litesync-free-ios-native-libs.tar.gz tar zxvf litesync-free-ios-native-libs.tar.gz cd .. cd ios && pod install && cd ..
These will also install the free version of LiteSync (native libraries for Android and iOS)
To run the project, open the emulator and run:
flutter run
For more information check the litesync_sqflite plugin page