Skip to content

Commit

Permalink
Use indexOf in managed list remove
Browse files Browse the repository at this point in the history
  • Loading branch information
nielsenko committed Sep 20, 2022
1 parent ff2ffcc commit b14a440
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/src/list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,17 @@ class ManagedRealmList<T extends Object?> with RealmEntity, ListMixin<T> impleme
}
}

@override
bool remove(covariant T element) {
if (element is RealmObject && !element.isManaged) {
throw RealmStateError('Cannot call remove on a managed list with an element that is an unmanaged object');
}
final index = indexOf(element);
final found = index > 0;
if (found) removeAt(index);
return found;
}

@override
T operator [](int index) {
if (index < 0) {
Expand Down
14 changes: 14 additions & 0 deletions test/list_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -716,6 +716,20 @@ Future<void> main([List<String>? args]) async {
expect(realm.write(() => team.players.remove(dan)), isFalse);

expect(team.players.every((p) => p.isValid && p.isManaged), isTrue);

expect(
() => (team.players as List<Object>).indexOf("wrong type"), // ignore: unnecessary_cast
throwsA(isA<TypeError>()),
);

expect(
() => team.players.remove(Person('alice')),
throwsA(isA<RealmStateError>().having(
(e) => e.message,
'message',
'Cannot call remove on a managed list with an element that is an unmanaged object',
)),
);
});

test('ManagedRealmList.setRange', () {
Expand Down

0 comments on commit b14a440

Please sign in to comment.