debugCheckHasDirectionality function

bool debugCheckHasDirectionality (BuildContext context)

Asserts that the given context has a Directionality ancestor.

Used by various widgets to make sure that they are only used in an appropriate context.

To invoke this function, use the following pattern, typically in the relevant Widget's build method:

assert(debugCheckHasDirectionality(context));

Does nothing if asserts are disabled. Always returns true.

Implementation

bool debugCheckHasDirectionality(BuildContext context) {
  assert(() {
    if (context.widget is! Directionality && context.ancestorWidgetOfExactType(Directionality) == null) {
      final Element element = context;
      throw FlutterError(
        'No Directionality widget found.\n'
        '${context.widget.runtimeType} widgets require a Directionality widget ancestor.\n'
        'The specific widget that could not find a Directionality ancestor was:\n'
        '  ${context.widget}\n'
        'The ownership chain for the affected widget is:\n'
        '  ${element.debugGetCreatorChain(10)}\n'
        'Typically, the Directionality widget is introduced by the MaterialApp '
        'or WidgetsApp widget at the top of your application widget tree. It '
        'determines the ambient reading direction and is used, for example, to '
        'determine how to lay out text, how to interpret "start" and "end" '
        'values, and to resolve EdgeInsetsDirectional, '
        'AlignmentDirectional, and other *Directional objects.'
      );
    }
    return true;
  }());
  return true;
}