在Angular中,可以使用canActivate进行路线防护。
canActivate
在Flutter中,该如何处理?守卫在哪里?您如何守护路线?
我在考虑以下方面:
我也在这个问题上绊脚石,最终FutureBuilder为此使用了一个。看看我的路线:
FutureBuilder
final routes = { '/': (BuildContext context) => FutureBuilder<AuthState>( // This is my async call to sharedPrefs future: AuthProvider.of(context).authState$.skipWhile((_) => _ == null).first, builder: (BuildContext context, AsyncSnapshot<AuthState> snapshot) { switch(snapshot.connectionState) { case ConnectionState.done: // When the future is done I show either the LoginScreen // or the requested Screen depending on AuthState return snapshot.data == AuthState.SIGNED_IN ? JobsScreen() : LoginScreen() default: // I return an empty Container as long as the Future is not resolved return Container(); } }, ), };
如果要跨多个路径重用代码,则可以扩展FutureBuilder。