go_router实用技巧记录

87 阅读1分钟

版本号: 12.0.0

  • 返回到指定location
  void popToLocation(String location) {
    while (getContext().canPop()) {
      getContext().pop();
      final RouteMatch lastMatch = kRouter.routerDelegate.currentConfiguration.last;
      final RouteMatchList matchList = lastMatch is ImperativeRouteMatch ? lastMatch.matches : kRouter.routerDelegate.currentConfiguration;
      final String targetLocation = matchList.uri.path;
      if (targetLocation == location) {
        break;
      }
    }
  }
  • 返回到根目录
 void popToRoot() {
    while (Router.getContext().canPop()) {
      Router.getContext().pop();
    }
  }