Missing .container member - incorrect documentation example, package version 4.1.0
Closed this issue · 2 comments
s6o commented
Hey, thank you for a great library. I especially like that the generated code is also clean/free from warnings :-)
I'm playing around with decoding lists and came across a code sample in the documentation that actually does not work as described.
The issue is with the .container member of a <model>Mapper class. The member is not created. I've attached a simple dart only project that also demonstrates the issue.
The code sample in the documentation is in Encoding Lists, Sets and Maps. For convenience ref. below:
@MappableClass()
class Dog with DogMappable {
String name;
Dog(this.name);
}
@MappableClass()
class Box<T> with BoxMappable<T> {
T content;
Box(this.content);
}
void main() {
// Case 1: Simple list.
// We use the default container since we only use core types.
List<int> nums = MapperContainer.defaults.fromJson('[2, 4, 105]');
print(nums); // [2, 4, 105]
// Case 2: Set of objects.
// We use the generated container for [Dog], but with a set of objects.
Set<Dog> dogs = DogMapper.container.fromJson('[{"name": "Thor"}, {"name": "Lasse"}, {"name": "Thor"}]');
print(dogs); // {Dog(name: Thor), Dog(name: Lasse)}
// Case 3: More complex lists, like generics.
// We use the generated container for [Box], but with a list of generic objects.
List<Box<double>> boxes = BoxMapper.container.fromJson('[{"content": 0.1}, {"content": 12.34}]');
print(boxes); // [Box(content: 0.1), Box(content: 12.34)]
}
AFAIU the main
function should look like this:
// ...
void main(List<String> arguments) {
// Case 1: Simple list.
// We use the default container since we only use core types.
List<int> nums = MapperContainer.defaults.fromJson('[2, 4, 105]');
print(nums); // [2, 4, 105]
// Case 2: Set of objects.
// We use the generated container for [Dog], but with a set of objects.
MapperContainer.globals.use(DogMapper.ensureInitialized());
Set<Dog> dogs = MapperContainer.globals
.fromJson('[{"name": "Thor"}, {"name": "Lasse"}, {"name": "Thor"}]');
print(dogs);
// Case 3: More complex lists, like generics.
// We use the generated container for [Box], but with a list of generic objects.
MapperContainer.globals.use(BoxMapper.ensureInitialized());
List<Box<double>> boxes = MapperContainer.globals
.fromJson('[{"content": 0.1}, {"content": 12.34}]');
print(boxes);
}
schultek commented
Thanks, indeed the docs are outdated. I will update them.
schultek commented
Updated with v4.2.0