わかりにくくなることを防ぐためにRealmモデルとViewの部分を一緒にしてます。
まずはRealmのインストールをしてください。
ターミナルで下記を打ち込めばOKです。
flutter pub add realm
その後に下記をコピペで(クラス名は同じにするか、必要に応じて変えてください)
クラスは二つだけなので、長いように感じるかもですが、多分他のどこよりも簡単にデータ作れると思うので、お付き合いください。
main.dart
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:realm/realm.dart';
part 'main.g.dart';
@RealmModel()
class _Car {
late String make;
String? model;
int? kilometers = 500;
_Person? owner;
}
@RealmModel()
class _Person {
late String name;
int age = 1;
}
void main() {
print("Current PID $pid");
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
late Realm realm;
_MyAppState() {
final config = Configuration([Car.schema, Person.schema]);
realm = Realm(config);
}
int get carsCount => realm.all<Car>().length;
@override
void initState() {
var myCar = Car("Tesla", model: "Model Y", kilometers: 1);
realm.write(() {
// print('Adding a Car to Realm.');
var car = realm.add(Car("Tesla", owner: Person("John")));
// print("Updating the car's model and kilometers");
// car.model = "Model 3";
// car.kilometers = 5000;
//
// print('Adding another Car to Realm.');
// realm.add(myCar);
//
// print("Changing the owner of the car.");
// myCar.owner = Person("me", age: 18);
// print("The car has a new owner ${car.owner!.name}");
});
// print("Getting all cars from the Realm.");
// var cars = realm.all<Car>();
// print("There are ${cars.length} cars in the Realm.");
//
// var indexedCar = cars[0];
// print('The first car is ${indexedCar.make} ${indexedCar.model}');
//
// print("Getting all Tesla cars from the Realm.");
// var filteredCars = realm.all<Car>().query("make == 'Tesla'");
// print('Found ${filteredCars.length} Tesla cars');
super.initState();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: Center(
child: Text('Running on: ${Platform.operatingSystem}.\n\nThere are $carsCount cars in the Realm.\n'),
),
),
);
}
}
main.g.dart(こちらもコピペでOK)
クラス名は必要に応じて変えてください。
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'main.dart';
// **************************************************************************
// RealmObjectGenerator
// **************************************************************************
class Car extends _Car with RealmEntity, RealmObject {
static var _defaultsSet = false;
Car(
String make, {
String? model,
int? kilometers = 500,
Person? owner,
}) {
if (!_defaultsSet) {
_defaultsSet = RealmObject.setDefaults<Car>({
'kilometers': 500,
});
}
RealmObject.set(this, 'make', make);
RealmObject.set(this, 'model', model);
RealmObject.set(this, 'kilometers', kilometers);
RealmObject.set(this, 'owner', owner);
}
Car._();
@override
String get make => RealmObject.get<String>(this, 'make') as String;
@override
set make(String value) => RealmObject.set(this, 'make', value);
@override
String? get model => RealmObject.get<String>(this, 'model') as String?;
@override
set model(String? value) => RealmObject.set(this, 'model', value);
@override
int? get kilometers => RealmObject.get<int>(this, 'kilometers') as int?;
@override
set kilometers(int? value) => RealmObject.set(this, 'kilometers', value);
@override
Person? get owner => RealmObject.get<Person>(this, 'owner') as Person?;
@override
set owner(covariant Person? value) => RealmObject.set(this, 'owner', value);
@override
Stream<RealmObjectChanges<Car>> get changes =>
RealmObject.getChanges<Car>(this);
static SchemaObject get schema => _schema ??= _initSchema();
static SchemaObject? _schema;
static SchemaObject _initSchema() {
RealmObject.registerFactory(Car._);
return const SchemaObject(Car, [
SchemaProperty('make', RealmPropertyType.string),
SchemaProperty('model', RealmPropertyType.string, optional: true),
SchemaProperty('kilometers', RealmPropertyType.int, optional: true),
SchemaProperty('owner', RealmPropertyType.object,
optional: true, linkTarget: 'Person'),
]);
}
}
class Person extends _Person with RealmEntity, RealmObject {
static var _defaultsSet = false;
Person(
String name, {
int age = 1,
}) {
if (!_defaultsSet) {
_defaultsSet = RealmObject.setDefaults<Person>({
'age': 1,
});
}
RealmObject.set(this, 'name', name);
RealmObject.set(this, 'age', age);
}
Person._();
@override
String get name => RealmObject.get<String>(this, 'name') as String;
@override
set name(String value) => RealmObject.set(this, 'name', value);
@override
int get age => RealmObject.get<int>(this, 'age') as int;
@override
set age(int value) => RealmObject.set(this, 'age', value);
@override
Stream<RealmObjectChanges<Person>> get changes =>
RealmObject.getChanges<Person>(this);
static SchemaObject get schema => _schema ??= _initSchema();
static SchemaObject? _schema;
static SchemaObject _initSchema() {
RealmObject.registerFactory(Person._);
return const SchemaObject(Person, [
SchemaProperty('name', RealmPropertyType.string),
SchemaProperty('age', RealmPropertyType.int),
]);
}
}
あとは実行すればRealmのデータが作られてます。下記参照
コメントアウトしている部分は最低限Realmのデータが作られる部分だけあればいいかなと思いコメントしてあります。
コメントアウトを外せば、作られるRealmのデータが二つになるだけなので、気になる方はコメントアウトを外してもらえればと。
データの確認方法はこちらの記事で解説してますので、必要な方はこちらもどうぞ。