getChildByName<T extends GDisplayObject> method

T? getChildByName<T extends GDisplayObject>(
  1. String name
)

Returns the child object that exists with the specified name.

The search includes the current object and all of its children, recursively.

If the specified child does not exist, the method returns null.

Optionally, this method can also search children of child recursively.

The T generic parameter specifies the expected return type. If the type of the child that matches the specified name does not match the specified type, the method returns null.

Usage:

var container = GDisplayObjectContainer();
container.addChild(GSprite()..name = 'sprite');
var sprite = container.getChildByName<GSprite>('sprite');

Implementation

T? getChildByName<T extends GDisplayObject>(String name) {
  for (final child in children) {
    if (child.name == name) {
      return child as T;
    }
  }
  return null;
}