addNavigation<Result> method

  1. @protected
Future<Result>? addNavigation<Result>({
  1. String? routeName,
  2. dynamic arguments,
})

Adds navigation data to navigationStream.

Method arguments wrap with RouteData object and pass to navigationStream. Returns null if this Bloc was disposed. Returns a Future that completes to the result value when RouteData.resultConsumer function will be called. RouteData.resultConsumer can be called once and only once, otherwise StateError will be thrown. The 'Result' type argument is the type of the return value.

Implementation

@protected
Future<Result>? addNavigation<Result>({
  String? routeName,
  dynamic arguments,
}) {
  if (isDisposed) {
    return null;
  }
  final resultCompleter = Completer<Result>();
  _navigationController.add(RouteData(
    name: routeName,
    arguments: arguments,
    resultConsumer: (Future? result) {
      if (resultCompleter.isCompleted) {
        throw StateError(
            'Navigation result has been already returned. This error has occurred because several Routers try to handle same navigation action. To avoid it try to use precondition functions in your BlocProvider or RouteListener.');
      } else {
        resultCompleter.complete(result?.then((value) {
          try {
            return value as Result;
          } catch (e) {
            throw ArgumentError('Result value type is ${value.runtimeType}, '
                '$Result expected. Please, check addNavigation() method call.');
          }
        }));
      }
    },
  ));
  return resultCompleter.future;
}