|
11 | 11 | * No need for client-side database migrations - these are handled automatically.
|
12 | 12 | * Subscribe to queries for live updates.
|
13 | 13 |
|
| 14 | +## Examples |
| 15 | + |
| 16 | +For complete app examples, see our [example app gallery](https://docs.powersync.com/resources/demo-apps-example-projects#flutter) |
| 17 | + |
| 18 | +For examples of some common patterns, see our [example snippets](./example/README.md) |
| 19 | + |
14 | 20 | ## Getting started
|
15 | 21 |
|
| 22 | +You'll need to create a PowerSync account and set up a PowerSync instance. You can do this at [https://www.powersync.com/](https://www.powersync.com/). |
| 23 | + |
| 24 | +### Install the package |
| 25 | + |
| 26 | +`flutter pub add powersync` |
| 27 | + |
| 28 | +### Implement a backend connector and initialize the PowerSync database |
| 29 | + |
16 | 30 | ```dart
|
17 | 31 | import 'package:powersync/powersync.dart';
|
18 | 32 | import 'package:path_provider/path_provider.dart';
|
19 | 33 | import 'package:path/path.dart';
|
20 | 34 |
|
| 35 | +// Define the schema for the local SQLite database. |
| 36 | +// You can automatically generate this schema based on your sync rules: |
| 37 | +// In the PowerSync dashboard, right-click on your PowerSync instance and then click "Generate client-side schema" |
21 | 38 | const schema = Schema([
|
22 | 39 | Table('customers', [Column.text('name'), Column.text('email')])
|
23 | 40 | ]);
|
24 | 41 |
|
25 | 42 | late PowerSyncDatabase db;
|
26 | 43 |
|
27 |
| -// Setup connector to backend if you would like to sync data. |
28 |
| -class BackendConnector extends PowerSyncBackendConnector { |
| 44 | +// You must implement a backend connector to define how PowerSync communicates with your backend. |
| 45 | +class MyBackendConnector extends PowerSyncBackendConnector { |
29 | 46 | PowerSyncDatabase db;
|
30 | 47 |
|
31 |
| - BackendConnector(this.db); |
| 48 | + MyBackendConnector(this.db); |
32 | 49 | @override
|
33 | 50 | Future<PowerSyncCredentials?> fetchCredentials() async {
|
34 |
| - // implement fetchCredentials |
| 51 | + // implement fetchCredentials to obtain a JWT from your authentication service |
| 52 | + // see https://docs.powersync.com/usage/installation/authentication-setup |
35 | 53 | }
|
36 | 54 | @override
|
37 | 55 | Future<void> uploadData(PowerSyncDatabase database) async {
|
38 |
| - // implement uploadData |
| 56 | + // Implement uploadData to send local changes to your backend service |
| 57 | + // You can omit this method if you only want to sync data from the server to the client |
| 58 | + // see https://docs.powersync.com/usage/installation/upload-data |
39 | 59 | }
|
40 | 60 | }
|
41 | 61 |
|
42 | 62 | openDatabase() async {
|
43 | 63 | final dir = await getApplicationSupportDirectory();
|
44 | 64 | final path = join(dir.path, 'powersync-dart.db');
|
| 65 | +
|
45 | 66 | // Setup the database.
|
46 | 67 | db = PowerSyncDatabase(schema: schema, path: path);
|
47 | 68 | await db.initialize();
|
48 | 69 |
|
49 |
| - // Run local statements. |
50 |
| - await db.execute( |
| 70 | + // Connect to backend |
| 71 | + db.connect(connector: MyBackendConnector(db)); |
| 72 | +} |
| 73 | +``` |
| 74 | + |
| 75 | +### Subscribe to changes in data |
| 76 | + |
| 77 | +```dart |
| 78 | +StreamBuilder( |
| 79 | + // you can watch any SQL query |
| 80 | + stream: return db.watch('SELECT * FROM customers order by id asc'), |
| 81 | + builder: (context, snapshot) { |
| 82 | + if (snapshot.hasData) { |
| 83 | + // TODO: implement your own UI here based on the result set |
| 84 | + return ...; |
| 85 | + } else { |
| 86 | + return const Center(child: CircularProgressIndicator()); |
| 87 | + } |
| 88 | + }, |
| 89 | +) |
| 90 | +``` |
| 91 | + |
| 92 | +### Insert, update, and delete data in the SQLite database as you would normally |
| 93 | + |
| 94 | +```dart |
| 95 | +FloatingActionButton( |
| 96 | + onPressed: () async { |
| 97 | + await db.execute( |
51 | 98 | 'INSERT INTO customers(id, name, email) VALUES(uuid(), ?, ?)',
|
52 |
| - |
| 99 | + |
| 100 | + ); |
| 101 | + }, |
| 102 | + tooltip: '+', |
| 103 | + child: const Icon(Icons.add), |
| 104 | +); |
| 105 | +``` |
53 | 106 |
|
| 107 | +### Send changes in local data to your backend service |
54 | 108 |
|
55 |
| - // Connect to backend |
56 |
| - db.connect(connector: BackendConnector(db)); |
| 109 | +```dart |
| 110 | +// Implement the uploadData method in your backend connector |
| 111 | +@override |
| 112 | +Future<void> uploadData(PowerSyncDatabase database) async { |
| 113 | + final batch = await database.getCrudBatch(); |
| 114 | + if (batch == null) return; |
| 115 | + for (var op in batch.crud) { |
| 116 | + switch (op.op) { |
| 117 | + case UpdateType.put: |
| 118 | + // Send the data to your backend service |
| 119 | + // replace `_myApi` with your own API client or service |
| 120 | + await _myApi.put(op.table, op.opData!); |
| 121 | + break; |
| 122 | + default: |
| 123 | + // TODO: implement the other operations (patch, delete) |
| 124 | + break; |
| 125 | + } |
| 126 | + } |
| 127 | + await batch.complete(); |
57 | 128 | }
|
58 | 129 | ```
|
| 130 | + |
| 131 | +### Logging |
| 132 | + |
| 133 | +You can enable logging to see what's happening under the hood |
| 134 | +or to debug connection/authentication/sync issues. |
| 135 | + |
| 136 | +```dart |
| 137 | +Logger.root.level = Level.INFO; |
| 138 | +Logger.root.onRecord.listen((record) { |
| 139 | + if (kDebugMode) { |
| 140 | + print('[${record.loggerName}] ${record.level.name}: ${record.time}: ${record.message}'); |
| 141 | +
|
| 142 | + if (record.error != null) { |
| 143 | + print(record.error); |
| 144 | + } |
| 145 | + if (record.stackTrace != null) { |
| 146 | + print(record.stackTrace); |
| 147 | + } |
| 148 | + } |
| 149 | +}); |
| 150 | +``` |
| 151 | + |
0 commit comments