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™.
This project is in the Beta stage. The API should be quite stable, but occasional breaking changes may be made.
-
Import Realm in a dart file
app.dart
import 'package:realm/realm.dart'; // import realm package part 'app.g.dart'; // declare a part file. @RealmModel() // define a data model class named `_Car`. class _Car { late String make; late String model; int? kilometers = 500; }
-
Generate RealmObject class
Car
from data model class_Car
.flutter pub run realm generate
-
Open a Realm and add some objects.
var config = Configuration.local([Car.schema]); var realm = Realm(config); var car = Car("Tesla", "Model Y", kilometers: 5); realm.write(() { realm.add(car); });
-
Query objects in Realm.
var cars = realm.all<Car>(); Car myCar = cars[0]; print("My car is ${myCar.make} model ${myCar.model}"); cars = realm.all<Car>().query("make == 'Tesla'");
-
Get stream of result changes for a query.
final cars = realm.all<Car>().query(r'make == $0', ['Tesla']); cars.changes.listen((changes) { print('Inserted indexes: ${changes.inserted}'); print('Deleted indexes: ${changes.deleted}'); print('Modified indexes: ${changes.modified}'); }); realm.write(() => realm.add(Car('VW', 'Polo', kilometers: 22000)));
For complete samples check the Realm Flutter and Dart Samples.
For API documentation go to
For a complete documentation go to Realm Flutter and Dart SDK Docs.
-
It provides the functionality for creating, retrieving, querying, sorting, filtering, updating Realm objects.
-
Migrations are not supported yet.
If you change your data models often and receive a migration exception be sure to delete the old
default.realm
file in your application directory. It will get recreated with the new schema the next time the Realm is opened.
The Realm Flutter package name is realm
-
Supported platforms are Flutter (iOS, Android, Windows, MacOS) and Dart standalone (Windows, MacOS and Linux)
-
Flutter ^3.0
-
For Flutter Desktop environment setup check the guide here
-
Cocoapods v1.11 or newer
-
CMake 3.21 or newer
The full contents of catalog.dart
is listed after the usage
-
Add
realm
package to a Flutter application.flutter pub add realm
-
Import Realm in a dart file (ex.
catalog.dart
).import 'package:realm/realm.dart';
-
Declare a part file
catalog.g.dart
in the begining of thecatalog.dart
dart file after all imports.import 'dart:io'; part 'catalog.g.dart';
-
Create a data model class.
It should start with an underscore
_Item
and be annotated with@RealmModel()
@RealmModel() class _Item { @PrimaryKey() late int id; late String name; int price = 42; }
-
Generate RealmObject class
Item
from data model class_Item
.On Flutter use
flutter pub run realm
to runrealm
package commandsflutter pub run realm generate
A new file
catalog.g.dart
will be created next to thecatalog.dart
.*This file should be committed to source control
-
Use the RealmObject class
Item
with Realm.// Create a Configuration object var config = Configuration.local([Item.schema]); // Opean 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();
import 'package:realm/realm.dart';
part 'catalog.g.dart';
@RealmModel()
class _Item {
@PrimaryKey()
late int id;
late String name;
int price = 42;
}
// Create a Configuration object
var config = Configuration.local([Item.schema]);
// Opean 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();
The Realm Dart package is realm_dart
-
Supported platforms are Windows, Mac and Linux.
-
Dart SDK ^2.17
-
Add
realm_dart
package to a Dart application.dart pub add realm_dart
-
Install the
realm_dart
package into the application. This downloads and copies the required native binaries to the app directory.dart run realm_dart install
-
Import realm_dart in a dart file (ex.
catalog.dart
).import 'package:realm_dart/realm.dart';
-
To generate RealmObject classes with realm_dart use this command.
On Dart use
dart run realm_dart
to runrealm_dart
package commandsdart run realm_dart generate
A new file
catalog.g.dart
will be created next to thecatalog.dart
.*This file should be committed to source control
-
For more usage of Realm Dart see the Realm Flutter usage above.
See CONTRIBUTING.md for instructions about building the source.
See test/README.md for instructions on running tests.
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.
Realm Flutter and Dart SDKs and Realm Core are published under the Apache License 2.0.