Home

Awesome

[!WARNING] We announced the deprecation of Atlas Device Sync + Realm SDKs in September 2024. For more information please see:

For a version of realm-dart without sync features, install version 20 or see the community branch.

<picture> <source srcset="./media/logo-dark.svg" media="(prefers-color-scheme: dark)" alt="realm by MongoDB"> <img src="./media/logo.svg" alt="realm by MongoDB"> </picture>

License Realm Dart CI Coverage Status

Realm is a mobile database that runs directly inside phones, tablets or wearables. This repository holds the source code for the Realm SDK for Flutter™ and Dart™.

Features

Getting Started

Samples

For complete samples check the Realm Flutter and Dart Samples.

Documentation

For API documentation go to

Use realm package for Flutter and realm_dart package for Dart applications.

For complete documentation of the SDKs, go to the Realm SDK documentation.

If you are using the Realm SDK for the first time, refer to the Quick Start documentation.

To learn more about using Realm with Atlas App Services and Device Sync, refer to the following Realm SDK documentation:

Realm Flutter SDK

Realm Flutter package is published to realm.

Environment setup for Realm Flutter

Usage

The full contents of catalog.dart is listed after the usage

Full contents of catalog.dart

import 'package:realm/realm.dart';

part 'catalog.realm.dart';

@RealmModel()
class _Item {
    @PrimaryKey()
    late int id;

    late String name;

    int price = 42;
}

// Create a Configuration object
var config = Configuration.local([Item.schema]);

// Open a Realm
var realm = Realm(config);

var myItem = Item(0, 'Pen', price: 4);

// Open a write transaction
realm.write(() {
    realm.add(myItem);
    var item = realm.add(Item(1, 'Pencil')..price = 20);
});

// Objects `myItem` and `item` are now managed and persisted in the realm

// Read object properties from realm
print(myItem.name);
print(myItem.price);

// Update object properties
realm.write(() {
    myItem.price = 20;
    myItem.name = "Special Pencil";
});

// Get objects from the realm

// Get all objects of type
var items = realm.all<Item>();

// Get object by index
var item = items[1];

// Get object by primary key
var itemByKey = realm.find<Item>(0);

// Filter and sort object
var objects = realm.query<Item>("name == 'Special Pencil'");
var name = 'Pen';
objects = realm.query<Item>(r'name == $0', [name]);

// Close the realm
realm.close();

Realm Dart Standalone SDK

Realm Dart package is published to realm_dart.

Environment setup for Realm Dart

Usage

Sync data with Realm Flutter and Dart using Device Sync

This section is about how to use the Realm with Device Sync and how to connect to Atlas App Services.

I. Set up Atlas App Services

  1. Create an account on cloud.mongodb.com. Follow the instructions: Register a new Atlas Account.
  2. Create a new App following the instructions here: Create an App with Atlas App Services UI.
  3. Read Authentication Providers to see how to configure the appropriate authentication provider type.
  4. Go to the Device Sync menu and Enable Flexible Sync.
  5. Find and Copy the App ID of your new application.

II. Use Device Sync with the Realm

  1. Initialize the App Services App client and authenticate a user.

    String appId = "<Atlas App ID>";
    final appConfig = AppConfiguration(appId);
    final app = App(appConfig);
    final user = await app.logIn(Credentials.anonymous());
    
  2. Open a synced realm.

    final config = Configuration.flexibleSync(user, [Task.schema]);
    final realm = Realm(config);
    
  3. Add a sync subscription and write data.

    Only data matching the query in the subscription will be synced to the server and only data matching the subscription will be downloaded to the local device realm file.

    realm.subscriptions.update((mutableSubscriptions) {
    mutableSubscriptions.add(realm.query<Task>(r'status == $0 AND progressMinutes == $1', ["completed", 100]));
    });
    await realm.subscriptions.waitForSynchronization();
    realm.write(() {
      realm.add(Task(ObjectId(), "Send an email", "completed", 4));
      realm.add(Task(ObjectId(), "Create a meeting", "completed", 100));
      realm.add(Task(ObjectId(), "Call the manager", "init", 2));
    });
    realm.close();
    

To learn more about how to sync data with Realm using Device Sync, refer to the Quick Start with Sync documentation.

Building the source

See CONTRIBUTING.md for instructions about building the source.

Code of Conduct

This project adheres to the MongoDB Code of Conduct. By participating, you are expected to uphold this code. Please report unacceptable behavior to community-conduct@mongodb.com.

License

Realm Flutter and Dart SDKs and Realm Core are published under the Apache License 2.0.

The "Dart" name and logo and the "Flutter" name and logo are trademarks owned by Google.
<img style="width: 0px; height: 0px;" src="https://3eaz4mshcd.execute-api.us-east-1.amazonaws.com/prod?s=https://github.com/realm/realm-dart#README.md">