addSection method
Inserts section at index (or at the end when null). If items
is non-empty they become the section's children — this initial
child population is structural and does not animate per item; the
section's own appearance respects animate.
Implementation
void addSection(
Section section, {
int? index,
Iterable<Item>? items,
bool animate = true,
}) {
_checkNotDisposed();
final sectionKey = sectionKeyOf(section);
// A section that is mid-EXIT is excluded: addSection on it is the
// documented re-include path, which cancels the deletion through
// insertRoot. `hasSection` reads the node data, which an exiting
// section still has.
if (hasSection(sectionKey) &&
!_tree.isPendingDeletion(SectionKey<K>(sectionKey))) {
throw ArgumentError(
"SectionedListController.addSection: section $sectionKey already "
"exists. Use setItems() or updateSection() instead.",
);
}
if (items != null) {
_requireItemsFree([for (final i in items) itemKeyOf(i)], "addSection");
}
_tree.runBatch(() {
_tree.insertRoot(
TreeNode(
key: SectionKey<K>(sectionKey),
data: SectionPayload<Section, Item>(section),
),
index: index,
animate: animate,
);
if (items != null) {
final children = <TreeNode<SecKey<K>, SecPayload<Section, Item>>>[
for (final i in items)
TreeNode(
key: ItemKey<K>(itemKeyOf(i)),
data: ItemPayload<Section, Item>(i),
),
];
if (children.isNotEmpty) {
_tree.setChildren(SectionKey<K>(sectionKey), children);
}
}
});
}