Docs Menu
Docs Home
/ /
Atlas Device SDKs
/
/

Add Device Sync to an App - Flutter SDK

On this page

  • Set up Device Sync
  • Configure Atlas Device Sync in App Services
  • Connect to the App Services Backend
  • Authenticate a user
  • Open a Synced Realm
  • Use the Realm
1

Before you can use Device Sync with the Realm Flutter SDK, you must create an Atlas App Services App with Device Sync and authentication enabled.

To learn how to set up Device Sync in your App, refer to Get Started with Atlas Device Sync in the App Services documentation.

To set up authentication, refer to Authenticate & Manage Users in the App Services documentation.

2

Initialize the an App instance to connect to your App Services App. Pass the App ID for your App, which you can find in the App Services UI.

final app = App(AppConfiguration(APP_ID));
3

Authenticate a user in your client project. This example uses anonymous authentication.

final user = await app.logIn(Credentials.anonymous());
4

Use a Flexible Sync configuration to open the realm as a synced realm. Also add a subscription to synchronize data matching the subscription query.

// Configure and open the realm
final config = Configuration.flexibleSync(user, [Car.schema]);
final realm = Realm(config);
// Add subscription to sync all Car objects in the realm
realm.subscriptions.update((mutableSubscriptions) {
mutableSubscriptions.add(realm.all<Car>());
});
// Sync all subscriptions
await realm.subscriptions.waitForSynchronization();

The syntax to read, write, and watch for changes on a synced realm is identical to the syntax for non-synced realms. While you work with local data, a background thread integrates, uploads, and downloads changesets.

The following code creates a new Car object and writes it to the realm:

// Write data to realm and it automatically syncs with Atlas
// in the background.
realm.write(() {
realm.add(Car(ObjectId(), 'Toyota', model: 'Rav 4'));
});
← 
 →