warn static method

bool warn(
  1. String message, [
  2. Object? data
])

Use this to log warnings to the console in dev mode only.

Code that produces the warnings will not be included when you compile in production mode.

assert(ValidationUtil.warn('Some warning message'));

Optionally, a component or element can be passed as data to provide additional information in the console.

assert(ValidationUtil.warn('Some warning message', component));

Assert that your component emits a warning using the validation test utilities available within package:over_react_test/over_react_test.dart like so:

group('emits a warning to the console', () {
  setUp(startRecordingValidationWarnings);

  tearDown(stopRecordingValidationWarnings);

  test('when <describe something that should trigger a warning>', () {
    // Do something that should trigger a warning

    verifyValidationWarning(/* some Matcher or String */);
  });

  test('unless <describe something that should NOT trigger a warning>', () {
    // Do something that should NOT trigger a warning

    rejectValidationWarning(/* some Matcher or String */);
  });
},
    // Be sure to not run these tests in dart2js the OverReact
    // ValidationUtil.warn() method will only produce a
    // console warning when asserts are enabled.
    tags: 'ddc');
);

Implementation

static bool warn(String message, [Object? data]) {
  WARNING_COUNT += 1;

  onWarning?.call(message);

  if (WARNINGS_ENABLED) {
    if (THROW_ON_WARNING) {
      throw ValidationWarning(message);
    }

    window.console.warn('VALIDATION WARNING: $message');

    if (data != null) {
        window.console.groupCollapsed('(Warning info)');
        window.console.log(data);

        if (isValidElement(data)) {
          window.console.log('props: ${prettyPrintMap(getProps(data))}');
        // ignore: deprecated_member_use
        } else if (data is react.Component) {
          window.console.log('props: ${data.props}');
        }

        window.console.groupEnd();
    }
  }

  return true;
}