within function Null safety Queries

WithinQueries within(
  1. Node node
)

Takes a DOM node and binds it to the raw query functions.

Can also be used to query within the ShadowRoot of an element by setting node to the Element.shadowRoot value as shown below:

// Or whatever query you want to use to find element(s) within the shadowRoot.
final fooNodeWithinShadowDom = within(someElement.shadowRoot).getByText('foo');

See: testing-library.com/docs/dom-testing-library/api-within

Implementation

WithinQueries within(Node node) {
  // Keep null check to maintain backwards compatibility for consumers that are not opted in to null safety.
  ArgumentError.checkNotNull(node);

  if (node is! ShadowRoot && !(node.isConnected ?? false)) {
    throw ArgumentError.value(
        node,
        'node',
        'The element you provide as the single argument to within() must exist in the DOM. '
            'Did you forget to append the element to the body?');
  }

  return WithinQueries._(node);
}