loadTreeRowChildren method

void loadTreeRowChildren(
  1. TreeRow treeRow,
  2. List<HeaderRow> headerRowList, {
  3. int level = 0,
  4. List<int> parentIndexes = const [],
})

Loads the children of a TreeRow lazily when it's expanded

Implementation

void loadTreeRowChildren(TreeRow treeRow, List<hr.HeaderRow> headerRowList,
    {int level = 0, List<int> parentIndexes = const []}) {
  final requestedSourceNode = treeRow.sourceNode;
  if (treeRow.childrenLoaded.value || requestedSourceNode == null || _loadingChildren.contains(treeRow)) {
    return;
  }

  if (requestedSourceNode.rows.isNotEmpty) {
    _materializeTreeRowChildren(
      treeRow,
      headerRowList,
      level: level,
      parentIndexes: parentIndexes,
    );
    return;
  }

  if (!requestedSourceNode.hasChildren && requestedSourceNode.rows.isEmpty) {
    return;
  }

  if (childrenLoader == null && queryChildrenLoader == null) {
    return;
  }

  _loadingChildren.add(treeRow);
  treeRow.childrenLoading!.value = true;
  final childLoadGeneration = _childLoadGeneration;
  var sourceWasReplaced = false;
  unawaited(Future.sync(() => _loadChildren(requestedSourceNode)).then((children) {
    if (_isDisposed || !_containsTreeRow(treeRow)) {
      return;
    }
    if (childLoadGeneration != _childLoadGeneration) {
      return;
    }
    if (!identical(treeRow.sourceNode, requestedSourceNode)) {
      sourceWasReplaced = true;
      return;
    }
    _validateLoadedChildren(children, headerRowList.last.columns.length);
    final currentSourceNode = treeRow.sourceNode;
    if (currentSourceNode == null) {
      return;
    }
    currentSourceNode
      ..rows = children
      ..hasChildren = children.isNotEmpty;
    _materializeTreeRowChildren(
      treeRow,
      headerRowList,
      level: level,
      parentIndexes: parentIndexes,
    );
    expansionChangeNotifier.notifyListeners();
  }).onError((Object _, StackTrace __) {
    // Keep the row unloaded so collapsing and expanding it retries the request.
  }).whenComplete(() {
    _loadingChildren.remove(treeRow);
    if (!_isDisposed && _containsTreeRow(treeRow)) {
      treeRow.childrenLoading!.value = false;
      if (sourceWasReplaced) {
        expansionChangeNotifier.notifyListeners();
      }
    }
  }));
}