describeMissingAncestor method

  1. @override
List<DiagnosticsNode> describeMissingAncestor(
  1. {required Type expectedAncestorType}
)
override

Adds a description of a specific type of widget missing from the current build context's ancestry tree.

You can find an example of using this method in debugCheckHasMaterial.

Implementation

@override
List<DiagnosticsNode> describeMissingAncestor({ required Type expectedAncestorType }) {
  final List<DiagnosticsNode> information = <DiagnosticsNode>[];
  final List<Element> ancestors = <Element>[];
  visitAncestorElements((Element element) {
    ancestors.add(element);
    return true;
  });

  information.add(DiagnosticsProperty<Element>(
    'The specific widget that could not find a $expectedAncestorType ancestor was',
    this,
    style: DiagnosticsTreeStyle.errorProperty,
  ));

  if (ancestors.isNotEmpty) {
    information.add(describeElements('The ancestors of this widget were', ancestors));
  } else {
    information.add(ErrorDescription(
      'This widget is the root of the tree, so it has no '
      'ancestors, let alone a "$expectedAncestorType" ancestor.',
    ));
  }
  return information;
}