invokeCallback<T> method Null safety

  1. @protected
T? invokeCallback<T>(
  1. String name,
  2. RecognizerCallback<T> callback,
  3. {String debugReport(
      )?}
    )
    protected">@protected

    Invoke a callback provided by the application, catching and logging any exceptions.

    The name argument is ignored except when reporting exceptions.

    The debugReport argument is optional and is used when debugPrintRecognizerCallbacksTrace is true. If specified, it must be a callback that returns a string describing useful debugging information, e.g. the arguments passed to the callback.

    Implementation

    @protected
    @pragma('vm:notify-debugger-on-exception')
    T? invokeCallback<T>(String name, RecognizerCallback<T> callback, { String Function()? debugReport }) {
      assert(callback != null);
      T? result;
      try {
        assert(() {
          if (debugPrintRecognizerCallbacksTrace) {
            final String? report = debugReport != null ? debugReport() : null;
            // The 19 in the line below is the width of the prefix used by
            // _debugLogDiagnostic in arena.dart.
            final String prefix = debugPrintGestureArenaDiagnostics ? '${' ' * 19}❙ ' : '';
            debugPrint('$prefix$this calling $name callback.${ (report?.isNotEmpty ?? false) ? " $report" : "" }');
          }
          return true;
        }());
        result = callback();
      } catch (exception, stack) {
        InformationCollector? collector;
        assert(() {
          collector = () => <DiagnosticsNode>[
            StringProperty('Handler', name),
            DiagnosticsProperty<GestureRecognizer>('Recognizer', this, style: DiagnosticsTreeStyle.errorProperty),
          ];
          return true;
        }());
        FlutterError.reportError(FlutterErrorDetails(
          exception: exception,
          stack: stack,
          library: 'gesture',
          context: ErrorDescription('while handling a gesture'),
          informationCollector: collector,
        ));
      }
      return result;
    }