Skip to content

Commit

Permalink
Fix removeAt
Browse files Browse the repository at this point in the history
  • Loading branch information
nielsenko committed Sep 5, 2022
1 parent 77e7305 commit 805ac44
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/src/list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ class ManagedRealmList<T extends Object?> extends collection.ListBase<T> with Re
}

@override
T removeAt(int index) {
final result = this[index];
realmCore.listRemoveElementAt(handle, index);
return result;
}

/// Removes all objects from this list; the length of the list becomes zero.
/// The objects are not deleted from the realm, but are no longer referenced from this list.
Expand Down
6 changes: 6 additions & 0 deletions lib/src/native/realm_core.dart
Original file line number Diff line number Diff line change
Expand Up @@ -824,6 +824,12 @@ class _RealmCore {
});
}

void listRemoveElementAt(RealmListHandle handle, int index) {
return using((Arena arena) {
_realmLib.invokeGetBool(() => _realmLib.realm_list_erase(handle._pointer, index));
});
}

void listDeleteAll(RealmList list) {
_realmLib.invokeGetBool(() => _realmLib.realm_list_remove_all(list.handle._pointer));
}
Expand Down
23 changes: 23 additions & 0 deletions test/list_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -361,4 +361,27 @@ Future<void> main([List<String>? args]) async {
final result = team.players.query(r'name BEGINSWITH $0', ['J']);
expect(result, [person]);
});

test('List removeAt', () {
final config = Configuration.local([Team.schema, Person.schema]);
final realm = getRealm(config);

final alice = Person('Alice');
final bob = Person('Bob');
final carol = Person('Carol');
final dan = Person('Dan');
final team = Team('Class of 92', players: [alice, bob, carol, dan]);

realm.write(() => realm.add(team));
expect(team.players.length, 4);
expect(team.players, [alice, bob, carol, dan]);

expect(realm.write(() => team.players.removeAt(2)), carol);
//expect(team.players.length, 3);
expect(team.players, [alice, bob, dan]);

expect(realm.write(() => team.players.removeAt(0)), alice);
expect(team.players.length, 2);
expect(team.players, [bob, dan]);
});
}

0 comments on commit 805ac44

Please sign in to comment.