@@ -8,48 +8,43 @@ import '../store.dart';
88import '../transaction.dart' ;
99import 'info.dart' ;
1010
11- /// A lazily loaded `List` of target objects representing a to-many relation,
12- /// a unidirectional link from a "source" entity to multiple objects of a
13- /// "target" entity.
14- ///
15- /// It tracks changes (adds and removes) that can be later applied (persisted)
16- /// to the database. This happens either when the source entity of this relation
17- /// is put or using [applyToDb] . For some important details about applying
18- /// changes, see the notes about relations of [Box.put] .
19- ///
20- /// The objects are loaded lazily on first access of this list, and then cached.
21- /// Subsequent calls to any method, like [length] , do not query the database,
22- /// even if the relation was changed elsewhere. To get the latest data [Box.get]
23- /// the source object again.
24- ///
25- /// You can:
26- /// - [add] new objects to the relation.
27- /// - [removeAt] target objects from the relation at a specific list index.
28- /// - [remove] target objects from the relation by an ID.
11+ /// A to-many relation of an entity that references multiple objects of a "target" entity [EntityT] .
2912///
13+ /// Example:
3014/// ```
15+ /// @Entity()
3116/// class Student {
3217/// final teachers = ToMany<Teacher>();
3318/// }
19+ /// ```
3420///
35- /// // Example 1: create a relation
36- /// final teacher1 = Teacher();
37- /// final teacher2 = Teacher();
21+ /// Implements the `List` interface and uses lazy initialization.
22+ /// The target objects are only read from the database when the list is first accessed.
3823///
39- /// final student1 = Student();
40- /// student1.teachers.add(teacher1);
41- /// student1.teachers.add(teacher2);
24+ /// Tracks when target objects are added and removed. Common usage:
25+ /// - [add] target objects to the relation.
26+ /// - [remove] target objects from the relation.
27+ /// - [removeAt] target objects at a specific index.
4228///
43- /// final student2 = Student();
44- /// student2.teachers.add(teacher2);
29+ /// To apply (persist) the changes to the database, call [applyToDb] or put the object with the ToMany.
30+ /// For important details, see the notes about relations of [Box.put] .
4531///
46- /// // saves students as well as teachers in the database
47- /// store.box<Student>().putMany([student1, student2]);
32+ /// ```
33+ /// // Example 1: add target objects to a relation
34+ /// student.teachers.add(teacher1);
35+ /// student.teachers.add(teacher2);
36+ /// store.box<Student>().put(student);
4837///
49- /// // Example 2: remove a relation
50- /// student.teachers.removeAt(index)
51- /// student.teachers.applyToDb(); // or store.box<Student>().put(student);
38+ /// // Example 2: remove a target object from the relation
39+ /// student.teachers.removeAt(index);
40+ /// student.teachers.applyToDb();
41+ /// // or store.box<Student>().put(student);
5242/// ```
43+ ///
44+ /// In the database, the target objects are referenced by their IDs, which are
45+ /// persisted as part of the relation of the object with the ToMany.
46+ ///
47+ /// To get all objects with a ToMany that reference a target object, see [Backlink] .
5348class ToMany <EntityT > extends Object with ListMixin <EntityT > {
5449 /// Store-related configuration attached to this.
5550 ///
@@ -92,6 +87,9 @@ class ToMany<EntityT> extends Object with ListMixin<EntityT> {
9287 _items.length = newLength;
9388 }
9489
90+ /// Gets the target object at the given index.
91+ ///
92+ /// [ToMany] uses lazy initialization, so on first access this will read the target objects from the database.
9593 @override
9694 EntityT operator [](int index) => _items[index];
9795
@@ -109,6 +107,10 @@ class ToMany<EntityT> extends Object with ListMixin<EntityT> {
109107 _track (element, 1 );
110108 }
111109
110+ /// Prepares to add the given target object to this relation.
111+ ///
112+ /// To apply changes, call [applyToDb] or put the object with the ToMany.
113+ /// For important details, see the notes about relations of [Box.put] .
112114 @override
113115 void add (EntityT element) {
114116 ArgumentError .checkNotNull (element, 'element' );
@@ -121,6 +123,7 @@ class ToMany<EntityT> extends Object with ListMixin<EntityT> {
121123 }
122124 }
123125
126+ /// Like [add] , but for multiple target objects.
124127 @override
125128 void addAll (Iterable <EntityT > iterable) {
126129 iterable.forEach (_track);
@@ -132,8 +135,11 @@ class ToMany<EntityT> extends Object with ListMixin<EntityT> {
132135 }
133136 }
134137
135- // note: to override, arg must be "Object", same as in the base class.
136- @override
138+ /// Prepares to remove the target object from this relation.
139+ ///
140+ /// To apply changes, call [applyToDb] or put the object with the ToMany.
141+ /// For important details, see the notes about relations of [Box.put] .
142+ @override // note: to override, arg must be "Object", same as in the base class.
137143 bool remove (Object ? element) {
138144 if (! _items.remove (element)) return false ;
139145 if (element != null ) _track (element as EntityT , - 1 );
0 commit comments