Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add realm specific implementation for List.add and List.insert #894

Merged
merged 4 commits into from Sep 12, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -8,6 +8,7 @@
### Fixed
* Allow null arguments on query. ([#872](https://github.com/realm/realm-dart/pull/872)). Fixes [#871](https://github.com/realm/realm-dart/issues/871)
* Previously removeAt did not truncate length. ([#884](https://github.com/realm/realm-dart/pull/884)). Fixes [#883](https://github.com/realm/realm-dart/issues/883)
* List.length= now throws, if you try to increase length, ([#894](https://github.com/realm/realm-dart/pull/894)).

### Compatibility
* Realm Studio: 12.0.0 or later.
Expand Down
16 changes: 15 additions & 1 deletion lib/src/list.dart
Expand Up @@ -63,7 +63,11 @@ class ManagedRealmList<T extends Object?> extends collection.ListBase<T> with Re
@override
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
@override
/// Setting the `length` is a required method on [List], but makes less sense
/// for [RealmList]s. You can only decrease the length, increasing it throws an [RealmException].

set length(int newLength) {
var l = length;
if (newLength < l) removeRange(newLength, l);
if (newLength < l) {
removeRange(newLength, l);
} else {
throw RealmException('You cannot increase length on a realm list without adding elements');
}
}

@override
Expand Down Expand Up @@ -93,6 +97,16 @@ class ManagedRealmList<T extends Object?> extends collection.ListBase<T> with Re
}
}

@override
void add(T element) {
RealmListInternal.setValue(handle, realm, length, element);
}

@override
void insert(int index, T element) {
realmCore.listInsertElementAt(handle, index, element);
}

@override
void operator []=(int index, T value) {
RealmListInternal.setValue(handle, realm, index, value);
Expand Down
11 changes: 11 additions & 0 deletions test/list_test.dart
Expand Up @@ -699,4 +699,15 @@ Future<void> main([List<String>? args]) async {
expect(team.players, isNot(players));
expect(team.players, unorderedMatches(players));
});

test('ManagedRealmList.length= throws', () {
nielsenko marked this conversation as resolved.
Show resolved Hide resolved
final config = Configuration.local([Team.schema, Person.schema]);
final realm = getRealm(config);

final team = Team('sad team');

realm.write(() => realm.add(team));

expect(() => realm.write(() => team.players.length = 100), throws<RealmException>('You cannot increase length on a realm list without adding elements'));
});
}