maybeFindRoot<T> static method

T? maybeFindRoot<T>(
  1. BuildContext context
)

Optionally find the root data of the given type from the context.

  • T The type of the data.
  • context The build context.

Implementation

static T? maybeFindRoot<T>(BuildContext context) {
  assert(context.mounted, 'The context must be mounted');
  T? found;
  context.visitAncestorElements((element) {
    if (element.widget is Data<T>) {
      var data = (element.widget as Data<T>)._data;
      if (data != null) {
        found = data;
      }
    }
    return true;
  });
  return found;
}