onAboutToExpand property

void Function(TreeNode node)? onAboutToExpand
final

This method is called right before a TreeNode is expanded.

Allows to dynamically populate the TreeView. Example:

final rootNode = TreeNode(id: '#root')
  ..addChildren(
    fetchTopLevelNodesFromDatabase(),
  );

final treeController = TreeController(
  rootNode: rootNode,
  onAboutToExpand: (TreeNode nodeAboutToExpand) {
    if (nodeAboutToExpand == rootNode) {
      return;
    }
    final List<String> childrenIds = fetchChildrenOfNodeFromDatabase(
      nodeAboutToExpand.id,
    );

    if (childrenIds.isEmpty) {
      return;
    }

    nodeAboutToExpand.addChildren(
      childrenIds.map((String childId) {
        return TreeNode(id: childId);
      }),
    );
  },
);

No checks are done to node before calling this method, i.e. isExpanded.

Make sure this callback is synchronous, if you need to add child nodes asynchronously, use refreshNode instead.

Implementation

final void Function(TreeNode node)? onAboutToExpand;