Hi All I need a quick solution or update on the following. I am trying to add / update / delete items from realm and the changes are not getting populated in the database. Check below code that can help you all to understand.
Error : Realm object is not managed
Sample Code (Add Item to Existing list)
Future<void> addTracker() async {
var realm = Realm(realmConfig);
Items item = Items(id: 3, name: 't3');
realm.write(() {
final wo = get();
wo?.trackers.add(item);
});
realm.close();
}
If I print the list length after adding, I can get it correctly, but the changes are not reflected in the realm.
Generated Model
@RealmModel()
class _Orders {
int? id;
String? woName;
late List<$Items> trackers;
}
@RealmModel()
class $Items {
int? id;
String? name;
}
Don’t close the realm until you are done using the objects it serve. Also I’m not sure what you are trying to do with the map call?
I have tried re-writing your code the best of my understanding, in a way that works:
import 'package:realm_dart/realm.dart';
part 'forum241674.g.dart';
@RealmModel()
class _Orders {
@PrimaryKey()
late int id;
late String woName;
late List<$Items> trackers;
@override
String toString() => 'Orders{id: $id, woName: $woName, trackers: $trackers}';
}
@RealmModel()
class $Items {
// Prefer pseudo-random classes like ObjectId or Uuid over int for primary keys.
// It is hard to ensure uniqueness with int in a distributed system.
@PrimaryKey()
late ObjectId id;
late String name;
@override
String toString() => 'Items{id: $id, name: $name}';
}
final realmConfig = Configuration.local(
[Orders.schema, Items.schema],
shouldDeleteIfMigrationNeeded: true, // only for testing
);
// Don't open an close realms all the time. One per isolate will serve you well.
final realm = Realm(realmConfig);
Orders get() {
// This is a bit contrieved, but I'm just trying to follow your sample.
// It finds the first order, or create a new one if none exists.
// I assume you want more orders eventually.
return realm.all<Orders>().firstOrNull ?? realm.add(Orders(1, 'wo1'));
}
Future<void> addTracker() async {
final item = Items(ObjectId(), 'an item');
realm.write(() {
final wo = get();
wo.trackers.add(item);
});
}
Future<void> main(List<String> args) async {
await addTracker();
print(realm.all<Orders>());
// Don't close until you are done with all the objects served by the realm.
// In general you don't need to close realms explicitly
realm.close();
Realm.shutdown(); // only needed for non-Flutter apps, due to Dart VM issue.
}
It adds one new item to the one-and-only order on each run.
Sample output after a few runs:
(Orders{id: 1, woName: wo1, trackers: [Items{id: 64eced0fbda108e828d1dc82, name: an item}, Items{id: 64eced13abd6bb7b17ed0372, name: an item}, Items{id: 64eced18b0019cf0772a7e8b, name: an item}, Items{id: 64eced309533aea59065fec8, name: an item}, Items{id: 64eced3881db05fb23ff246a, name: an item}, Items{id: 64eced5e75f7626e497739f3, name: an item}, Items{id: 64ecee3e0601449d4414c33a, name: an item}]})
Exited.
It’s working, Realm Initialization & closing was the only issue. Moreover, the map I did was throwing invalidated or deleted errors but the same was fixed when only single initialization & close calls were carried out.