reportError method Null safety

void reportError(
  1. {DiagnosticsNode? context,
  2. required Object exception,
  3. StackTrace? stack,
  4. InformationCollector? informationCollector,
  5. bool silent = false}
)

Calls all the registered error listeners to notify them of an error that occurred while resolving the image.

If no error listeners (listeners with an ImageStreamListener.onError specified) are attached, or if the handlers all rethrow the exception verbatim (with throw exception), a FlutterError will be reported using FlutterError.reportError.

The context should be a string describing where the error was caught, in a form that will make sense in English when following the word "thrown", as in "thrown while obtaining the image from the network" (for the context "while obtaining the image from the network").

The exception is the error being reported; the stack is the StackTrace associated with the exception.

The informationCollector is a callback (of type InformationCollector) that is called when the exception is used by FlutterError.reportError. It is used to obtain further details to include in the logs, which may be expensive to collect, and thus should only be collected if the error is to be logged in the first place.

The silent argument causes the exception to not be reported to the logs in release builds, if passed to FlutterError.reportError. (It is still sent to error handlers.) It should be set to true if the error is one that is expected to be encountered in release builds, for example network errors. That way, logs on end-user devices will not have spurious messages, but errors during development will still be reported.

See FlutterErrorDetails for further details on these values.

Implementation

@pragma('vm:notify-debugger-on-exception')
void reportError({
  DiagnosticsNode? context,
  required Object exception,
  StackTrace? stack,
  InformationCollector? informationCollector,
  bool silent = false,
}) {
  _currentError = FlutterErrorDetails(
    exception: exception,
    stack: stack,
    library: 'image resource service',
    context: context,
    informationCollector: informationCollector,
    silent: silent,
  );

  // Make a copy to allow for concurrent modification.
  final List<ImageErrorListener> localErrorListeners = _listeners
      .map<ImageErrorListener?>((ImageStreamListener listener) => listener.onError)
      .whereType<ImageErrorListener>()
      .toList();

  bool handled = false;
  for (final ImageErrorListener errorListener in localErrorListeners) {
    try {
      errorListener(exception, stack);
      handled = true;
    } catch (newException, newStack) {
      if (newException != exception) {
        FlutterError.reportError(
          FlutterErrorDetails(
            context: ErrorDescription('when reporting an error to an image listener'),
            library: 'image resource service',
            exception: newException,
            stack: newStack,
          ),
        );
      }
    }
  }
  if (!handled) {
    FlutterError.reportError(_currentError!);
  }
}