我也在这个问题上绊脚石,最终
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。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)