push method

  1. @override
Future<Null> push (dynamic path, [ dynamic state ])
override

Push an entry (String or Location) on to the History list

You can optionally add a state object to add information about the entry that doesn't appear in the path.

This method will throw an ArgumentError if anything other than String or Location is given to it.

Implementation

@override
Future<Null> push(dynamic path, [dynamic state]) async {
  // Validate arguments
  validatePath(path);
  if (path is Location && path.state != null && state != null) {
    print(
        'WARNING: You should avoid adding the 2nd argument (state) when the 1st argument is a "Location" with a defined state; 2nd argument state will be ignored');
  }

  // Compute next location and action
  Location nextLocation = (path is String)
      ? Location(pathname: path, state: state, key: _createKey())
      : Location.copy((path as Location),
          key: _createKey(), state: path.state ?? state);
  var nextAction = Action.push;
  nextLocation.relateTo(_location);

  // Await Confirmation
  var ok = await _transitionManager.confirmTransitionTo(
      nextLocation, nextAction, _getConfirmation);
  if (!ok) {
    return;
  }

  var href = _href(nextLocation);

  if (_domUtils.supportsHistory) {
    if (_transitionManager.listeningToWindowEvents) {
      _popMode = PopMode.force;
    }
    _globalHistory.pushState(
        {'key': nextLocation.key, 'state': nextLocation.state}, null, href);

    if (_forceRefresh) {
      _window.location.href = href;
    } else {
      var prevIndex = _allKeys.indexOf(_location.key);
      _allKeys = _allKeys.sublist(0, prevIndex == -1 ? 0 : prevIndex + 1)
        ..add(nextLocation.key);
      _location = nextLocation;
      _action = nextAction;
      _transitionManager.notify(this);
    }
  } else {
    if (state != null) {
      print(
          'WARNING: Browser history cannot push state in browsers that do not support HTML5 history');
    }
    _window.location.href = href;
  }
}