'How to implement arguments to dynamic routing?

I'm trying to make dynamic routing in my flutter web app. But I can't figure out how to use arguments in dynamic routing.

    class Path {
  const Path(this.pattern, this.builder);

  /// A RegEx string for route matching.
  final String pattern;

  /// The builder for the associated pattern route. The first argument is the
  /// [BuildContext] and the second argument is a RegEx match if it is
  /// included inside of the pattern.
  final Widget Function(BuildContext, String) builder;
}

class RouteConfiguration {
  /// List of [Path] to for route matching. When a named route is pushed with
  /// [Navigator.pushNamed], the route name is matched with the [Path.pattern]
  /// in the list below. As soon as there is a match, the associated builder
  /// will be returned. This means that the paths higher up in the list will
  /// take priority.,

  static List<Path> paths = [
    Path(
      r'^' + AWidget.baseRoute + r'/([\w-]+)$',
      (context, match) => A.getAPage(match),
    ),
    Path(
      r'^' + BWidget.baseRoute + r'/([\w-]+)$',
      (context, match) => B.getBPage(match),
    ),
    Path(
      r'^' + HomePage.route,
      (context, match) => HomePage(),
    ),
  ];

  /// The route generator callback used when the app is navigated to a named
  /// route. Set it on the [MaterialApp.onGenerateRoute] or
  /// [WidgetsApp.onGenerateRoute] to make use of the [paths] for route
  /// matching.
  static Route<dynamic> onGenerateRoute(RouteSettings settings) {
    final ScreenArguments args = settings.arguments as ScreenArguments;
    for (Path path in paths) {
      final regExpPattern = RegExp(path.pattern);
      if (regExpPattern.hasMatch(settings.name)) {
        final firstMatch = regExpPattern.firstMatch(settings.name);
        final match = (firstMatch.groupCount == 1) ? firstMatch.group(1) : null;
        return MaterialPageRoute<void>(
          builder: (context) => path.builder(context, match),
          settings: settings,
        );
      }
    }

    // If no match was found, we let [WidgetsApp.onUnknownRoute] handle it.
    return MaterialPageRoute(
        builder: (_) => Scaffold(
              body: Center(child: Text('No route defined for ${settings.name}')),
            ));
  }
}

I tried to use this example https://docs.flutter.dev/cookbook/navigation/navigate-with-arguments But I can't mix how to use these arguments when having dynamic routing.



Solution 1:[1]

For example, you can pass argument to pushNamed or popAndPushNamed like this :

onTap: () {
              Navigator.popAndPushNamed(
                context,
                HomeView.routeName,
                arguments: {
                  'pageIndex': 0,
                },
              );
            },

and in destination widget you get argument like this :

  @override
  Widget build(BuildContext context) {
    final arguments = ModalRoute.of(context)!.settings.arguments;

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Alaindeseine