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
Cannot rebuild a BuiltSet entry in BuiltMap #249
Comments
`BuiltSet` is immutable, so rebuild does not change an existing instance;
you need to also assign:
```
orgBuilder.people['mobile'] =
orgBuilder.people['mobile']!
.rebuild((b) => b..removeWhere((e) => e.name == 'Luke'));
```
Usually a `BuiltSetMultimap` provides a nicer API here.
…On Tue, Jul 27, 2021, 23:46 Luke Pighetti ***@***.***> wrote:
It appears that if you try to rebuild a BuiltSet which is an entry of
BuiltMap, it does not rebuild as expected. The changes are not applied.
main.dart
import 'package:built_collection/built_collection.dart';import 'package:built_value_map_entry_rebuilder/models.dart';import 'package:flutter_test/flutter_test.dart';
void main() {
test('BuiltMap entry update', () {
final joe = Person((b) => b..name = 'Joe');
final luke = Person((b) => b..name = 'Luke');
final org = Organization((b) => b
..name = 'Foolandia'
..people = MapBuilder({
'mobile': BuiltSet<Person>({joe, luke}),
}));
final orgBuilder = org.toBuilder();
/// Initial works expect(orgBuilder.people['mobile'], equals(BuiltSet<Person>({joe, luke})));
/// Weird rebuild works orgBuilder.people['mobile'] = (orgBuilder.people['mobile']!.toBuilder()
..removeWhere((e) => e.name == 'Joe'))
.build();
expect(orgBuilder.people['mobile'], equals(BuiltSet<Person>({luke})));
/// Rebuild fails, expected to work orgBuilder.people['mobile']!
.rebuild((b) => b..removeWhere((e) => e.name == 'Luke'));
expect(orgBuilder.people['mobile'], equals(BuiltSet<Person>({})));
});
}
models.dart
import 'package:built_value/built_value.dart';import 'package:built_value/serializer.dart';import 'package:built_collection/built_collection.dart';
part 'models.g.dart';
abstract class Person implements Built<Person, PersonBuilder> {
static Serializer<Person> get serializer => _$personSerializer;
Person._();
factory Person([void Function(PersonBuilder) updates]) = _$Person;
String get name;
}
abstract class Organization
implements Built<Organization, OrganizationBuilder> {
static Serializer<Organization> get serializer => _$organizationSerializer;
Organization._();
factory Organization([void Function(OrganizationBuilder) updates]) =
_$Organization;
String get name;
BuiltMap<String, BuiltSet<Person>> get people;
}
built_value_test.dart
import 'package:built_collection/built_collection.dart';import 'package:built_value_map_entry_rebuilder/models.dart';import 'package:flutter_test/flutter_test.dart';
void main() {
test('BuiltMap entry update', () {
final joe = Person((b) => b..name = 'Joe');
final luke = Person((b) => b..name = 'Luke');
final org = Organization((b) => b
..name = 'Foolandia'
..people = MapBuilder({
'mobile': BuiltSet<Person>({joe, luke}),
}));
final orgBuilder = org.toBuilder();
/// Initial works expect(orgBuilder.people['mobile'], equals(BuiltSet<Person>({joe, luke})));
/// Weird rebuild works orgBuilder.people['mobile'] = (orgBuilder.people['mobile']!.toBuilder()
..removeWhere((e) => e.name == 'Joe'))
.build();
expect(orgBuilder.people['mobile'], equals(BuiltSet<Person>({luke})));
/// Rebuild fails, expected to work orgBuilder.people['mobile']!
.rebuild((b) => b..removeWhere((e) => e.name == 'Luke'));
expect(orgBuilder.people['mobile'], equals(BuiltSet<Person>({})));
});
}
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#249>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/ADAZMCR6C6WOYRFF75WXFJ3TZ4SLFANCNFSM5BDC7IGQ>
.
|
Does that mean that |
It returns a new result, it doesn't modify the existing collection. I think there's an annotation in |
It seems like there is an API divergence between built values and built collections. If you |
They're the same, |
Brain fart on my part. Thanks for sticking with me! |
No worries, this question comes up regularly :) ... I should try that annotation ... |
It appears that if you try to rebuild a BuiltSet which is an entry of BuiltMap, it does not rebuild as expected. The changes are not applied.
main.dart
models.dart
built_value_test.dart
Log
The text was updated successfully, but these errors were encountered: