Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

clarify readme #256

Merged
merged 4 commits into from
Feb 14, 2022
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 89 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ This repository holds the source code for the Realm SDK for Flutter™ and Dart

## Getting Started

* Import Realm.
```dart
import 'package:realm/realm.dart';
```
* Import Realm in a dart file `app.dart`

* Define a data model class `_Car`.
```dart
@RealmModel()
import 'package:realm/realm.dart'; // import realm package

part 'app.g.dart' // declare a part file.
blagoev marked this conversation as resolved.
Show resolved Hide resolved

@RealmModel() // define a data model class named `_Car`.
class _Car {
late String make;

Expand Down Expand Up @@ -90,17 +90,20 @@ The Realm Flutter package name is `realm`

* Flutter ^2.8.0
* For Flutter Desktop environment setup check the guide [here](https://docs.flutter.dev/desktop)
* Cocoapods v1.11 or newer is required

## Usage

**The full contents of `catalog.dart` is listed [after the usage](https://github.com/realm/realm-dart#full-contents-of-catalogdart)**

* Add `realm` package to a Flutter application.

```
flutter pub add realm
```

* Import Realm in a dart file (ex. `catalog.dart`).

* Import Realm in a dart file (ex. `catalog.dart`).
```dart
import 'package:realm/realm.dart';
```
Expand Down Expand Up @@ -149,11 +152,24 @@ The Realm Flutter package name is `realm`
// Opean a Realm
realm = Realm(config);
blagoev marked this conversation as resolved.
Show resolved Hide resolved

var myItem = Item(0, 'Item', price: 4);
blagoev marked this conversation as resolved.
Show resolved Hide resolved

// Open a write transaction
realm.write(() {
var myItem = Item(0, 'Iteam', price: 4);
realm.add(myItem);
var item = realm.add(Item(1, 'Iteam')..price = 20);
var item = realm.add(Item(1, 'Item')..price = 20);
blagoev marked this conversation as resolved.
Show resolved Hide resolved
});

// 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 = "New Item";
blagoev marked this conversation as resolved.
Show resolved Hide resolved
});

// Get objects from the realm
Expand All @@ -176,6 +192,69 @@ The Realm Flutter package name is `realm`
realm.close();
```

## Full contents of `catalog.dart`

```dart
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([Item.schema]);

// Opean a Realm
realm = Realm(config);
blagoev marked this conversation as resolved.
Show resolved Hide resolved

var myItem = Item(0, 'Item', price: 4);
blagoev marked this conversation as resolved.
Show resolved Hide resolved

// Open a write transaction
realm.write(() {
realm.add(myItem);
var item = realm.add(Item(1, 'Item')..price = 20);
blagoev marked this conversation as resolved.
Show resolved Hide resolved
});

// 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 = "New Item";
blagoev marked this conversation as resolved.
Show resolved Hide resolved
});

// Get objects from the realm

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

// Get object by index
var item = items[5];
blagoev marked this conversation as resolved.
Show resolved Hide resolved

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

// Filter and sort object
var objects = realm.query<Item>("name == 'Special Item'");
blagoev marked this conversation as resolved.
Show resolved Hide resolved
var name = 'John';
blagoev marked this conversation as resolved.
Show resolved Hide resolved
var objects = realm.query<Item>(r'name == $0', [name]]);
blagoev marked this conversation as resolved.
Show resolved Hide resolved

// Close the realm
realm.close();
```

# Realm Dart SDK

The Realm Dart package is `realm_dart`
Expand Down