当使用 get: ^5.0.0-release-candidate-9.2.1最新版本时,中间件GetMiddleware各种教程都是让我们在redirect中实现,比如:
@overrideRouteSettings? redirect(String? route) {return RouteSettings(name: "/companyAuthIndexPage");}
这个在之前版本是没有问题的,但是更新到最新版后,就无法生效了,查阅了官方后,得到了答案如下:
所有正确的姿势是:
@overrideFutureOr<RouteDecoder?> redirectDelegate(RouteDecoder route) {return RouteDecoder.fromRoute("/companyAuthIndexPage");}
当然作为合格的码农,怎么会止步于此,看了源代码后,发现不能生效的的代码如下:
bool needRecheck(BuildContext context) {if (settings == null && route != null) {settings = route;}final match = context.delegate.matchRoute(settings!.name!);// No Match foundif (match.route == null) {isUnknown = true;return false;}// No middlewares found return match.if (match.route!.middlewares.isEmpty) {return false;}final runner = MiddlewareRunner(match.route!.middlewares);route = runner.runOnPageCalled(match.route);addPageParameter(route!);final newSettings = runner.runRedirect(settings!.name);if (newSettings == null) {return false;}settings = newSettings;return true;}
关键在于赋值后的settings没有正常下发,所以即使redirect被调用,但是功能不会生效