@Collection
class Parent {
Parent({this.name,
this.age,
this.gender,
})
: List<child> = List.empty(growable: true);
Id id = Isar.autoIncrement;
String? name;
int? age;
String? gender;
List<child> children;
}
@embedded
class child {
child({this.name, this.age, this.gender});
String? name;
int? age;
String? gender;
}
위와 같은 컬렉션이 존재할 때, 아래와 같이 children 에 child 를 넣고자 하면 Unsupported operation: Cannot add to a fixed-length list
오류 발생
Parent? parent = Isar.parents.get(123);
parent.children.add(child(name: '홍길동', age: '20', gender: 'M');
Unsupported operation: Cannot add to a fixed-length list
생성자에서 children
을 List.empty(growable: true) 로 지정해주었으나, Isar 에서 꺼내올 때에는 고정된 길이의 List 를 가져오므로 위와 같은 오류가 발생한다.
오류를 해결하기 위해서는 children.toList()
메소드를 호출하여 길이가 늘어날 수 있도록 한 뒤 add 하면 된다.
Parent? parent = Isar.parents.get(123);
List<child> children = parent.children.toList();
children.add(child(name: '홍길동', age: '20', gender: 'M');
parent.children = children;
Isar.parents.put(parent);
List<E> toList({bool growable = true}) =>
List<E>.of(this, growable: growable);
List.toList()
메소드를 호출할 때 growable
파라미터를 지정하지 않으면 true
로 초기화되고, List.of
메소드를 호출할 때, growable
파라미터를 넘겨주어 길이가 늘어날 수 있는 List 를 생성한다.