generateRouteFactory function Null safety

RouteFactory generateRouteFactory(
  1. Iterable<StaticRoute> routes
)

生成静态路由工厂

构建根MaterialApp时需要在MaterialApp.onGenerateRoute上绑定此方法后静态路由表才能生效


final routeTable = <StaticRoute>[
 ExamplePage.route,
 ...
];

MaterialApp(
 onGenerateRoute: generateRouteFactory(routeTable),
);

Implementation

RouteFactory generateRouteFactory(Iterable<StaticRoute> routes) {
  final table = {for (var route in routes) route.name: route.builder};

  return (settings) =>
      table[settings.name]?.call(settings) ??
      MaterialPageRoute(
        settings: settings,
        builder: (context) =>
            Center(child: Text('${settings.name} route unregistered')),
      );
}