findParent<T> method Null safety

T findParent<T>()

Looks up the widget tree until it finds a parent of this type or otherwise throws an exception. Make sure that this widget has been appended by the appendTo() method first.

Implementation

T findParent<T>() {
  final parentPointer = parent;
  if (parentPointer == null) {
    throw Exception(
        'Unable to find parent of type $T in widget tree. Have you appended this widget with `.build()` instead of `.appendTo(this)` maybe?');
  }
  if (parentPointer is T) {
    return parentPointer as T;
  }
  return parentPointer.findParent<T>();
}