Line data Source code
1 : part of flutter_data; 2 : 3 : /// A [Relationship] that models a to-one ownership. 4 : /// 5 : /// Example: A book that belongs to an author 6 : /// ``` 7 : /// class Book with DataModel<Book> { 8 : /// @override 9 : /// final int id; 10 : /// final String title; 11 : /// final BelongsTo<Author> author; 12 : /// 13 : /// Todo({this.id, this.title, this.author}); 14 : /// } 15 : ///``` 16 : class BelongsTo<E extends DataModel<E>> extends Relationship<E, E?> { 17 : /// Creates a [BelongsTo] relationship, with an optional initial [E] model. 18 : /// 19 : /// Example: 20 : /// ``` 21 : /// final author = Author(name: 'JK Rowling'); 22 : /// final book = Book(id: 1, author: BelongsTo(author)); 23 : /// ``` 24 : /// 25 : /// See also: [DataModelRelationshipExtension<E>.asBelongsTo] 26 3 : BelongsTo([E? model]) : super(model != null ? {model} : null); 27 : 28 2 : BelongsTo._(Set<String>? keys) : super._(keys); 29 : 30 2 : BelongsTo.remove() : super._remove(); 31 : 32 : /// For internal use with `json_serializable`. 33 1 : factory BelongsTo.fromJson(final Map<String, dynamic> map) { 34 2 : if (map['_'] == null) return BelongsTo._(null); 35 2 : return BelongsTo._({...map['_']}); 36 : } 37 : 38 : /// Obtains the single [E] value of this relationship (`null` if not present). 39 3 : E? get value => isNotEmpty ? first : null; 40 : 41 : /// Sets the single [E] value of this relationship, replacing any previous [value]. 42 : /// 43 : /// Passing in `null` will remove the existing value from the relationship. 44 1 : set value(E? newValue) { 45 1 : final isAddition = value == null && newValue != null; 46 1 : final isUpdate = value != null && newValue != null; 47 1 : final isRemoval = value != null && newValue == null; 48 : 49 : if (isRemoval || isUpdate) { 50 2 : super.remove(value!, notify: false); 51 : } 52 : if (isAddition || isUpdate) { 53 1 : super.add(newValue!, notify: false); 54 : } 55 : 56 : // handle events 57 : DataGraphEventType? eventType; 58 : if (isAddition) eventType = DataGraphEventType.addEdge; 59 : if (isUpdate) eventType = DataGraphEventType.updateEdge; 60 : if (isRemoval) eventType = DataGraphEventType.removeEdge; 61 : 62 : if (eventType != null) { 63 2 : _graph._notify( 64 3 : [_ownerKey!, if (newValue != null) newValue._key], 65 1 : metadata: _name, 66 : type: eventType, 67 : ); 68 : } 69 : assert(length <= 1); 70 : } 71 : 72 : /// Returns the [value]'s `key` 73 1 : @protected 74 : @visibleForTesting 75 2 : String? get key => super.keys.safeFirst; 76 : 77 3 : String? get id => super.ids.safeFirst; 78 : 79 : /// Returns a [StateNotifier] which emits the latest [value] of 80 : /// this [BelongsTo] relationship. 81 1 : @override 82 : DelayedStateNotifier<E?> watch() { 83 3 : return _relationshipEventNotifier.map((e) { 84 1 : return [DataGraphEventType.removeNode, DataGraphEventType.removeEdge] 85 2 : .contains(e.type) 86 : ? null 87 1 : : value; 88 : }); 89 : } 90 : 91 1 : @override 92 : String toString() { 93 2 : return 'BelongsTo<$E>(${super.toString()})'; 94 : } 95 : } 96 : 97 : extension DataModelRelationshipExtension<T extends DataModel<T>> 98 : on DataModel<T> { 99 : /// Converts a [DataModel<T>] into a [BelongsTo<T>]. 100 : /// 101 : /// Equivalent to using the constructor as `BelongsTo(model)`. 102 2 : BelongsTo<T> get asBelongsTo => BelongsTo<T>(this as T); 103 : }