bySemanticsIdentifier method

Finder bySemanticsIdentifier(
  1. Pattern identifier, {
  2. bool skipOffstage = true,
})

Finds Semantics widgets matching the given identifier, either by RegExp.hasMatch or string equality.

This allows matching against the identifier of a Semantics widget, which is a unique identifier for the widget in the semantics tree. This is exposed to offer a unified way widget tests and e2e tests can match against a Semantics widget.

Sample code

expect(find.bySemanticsIdentifier('Back'), findsOneWidget);

If the skipOffstage argument is true (the default), then this skips nodes that are Offstage or that are from inactive Routes.

Implementation

Finder bySemanticsIdentifier(Pattern identifier, {bool skipOffstage = true}) {
  final String description = switch (identifier) {
    final RegExp regExp => 'a semantics identifier matching the pattern "${regExp.pattern}"',
    final String id => 'a semantics identifier named "$id"',
    _ => 'a semantics identifier matching "$identifier"',
  };

  return _bySemanticsProperty(
    identifier,
    description,
    (SemanticsNode? semantics) => semantics?.identifier,
    skipOffstage: skipOffstage,
  );
}