debugUsesDefaultChangeDetection function

  1. @experimental
bool debugUsesDefaultChangeDetection(
  1. ComponentRef<void> componentRef
)

Returns whether componentRef uses ChangeDetectionStrategy.Default.

In practice this can be used to assert that a component does not use default change detection in non-default or performance sensitive contexts.

final componentRef = viewContainerRef.createComponent(componentFactory);
assert(!debugUsesDefaultChangeDetection(componentRef));

Note that at runtime, we can only tell whether a component uses default change detection or not. It's not possible to distinguish which non-default change detection strategy is used because they all use the same runtime representation.

Implementation

@experimental
bool debugUsesDefaultChangeDetection(ComponentRef<void> componentRef) {
  if (!isDevMode) {
    throw StateError(
      'This function should only be used for assertions. Consider wrapping the '
      'invocation in an "assert()" statement.\n'
      '\n'
      'See "debugUsesDefaultChangeDetection()" documentation for details.',
    );
  }
  return componentRef._hostView.componentView.usesDefaultChangeDetection;
}