Flutter future builder中的颤振初始路径仅设置一次,并且不会重新渲染零部件

Flutter future builder中的颤振初始路径仅设置一次,并且不会重新渲染零部件,flutter,dart,Flutter,Dart,在这里,我试图有条件地呈现组件,这样,如果用户数据在安全存储中,但当应用程序启动时,它总是转到else语句,因为令牌尚未检索,但即使在重试之后,组件重新呈现路由也不会更改,它位于AuthScreen上 类MyApp扩展StatefulWidget{ @凌驾 _MyAppState createState()=>\u MyAppState(); } 类MyAppState扩展了状态{ @凌驾 小部件构建(构建上下文){ 回归未来建设者( future:TokenStorageService.jw

在这里,我试图有条件地呈现组件,这样,如果用户数据在安全存储中,但当应用程序启动时,它总是转到else语句,因为令牌尚未检索,但即使在重试之后,组件重新呈现路由也不会更改,它位于AuthScreen上

类MyApp扩展StatefulWidget{
@凌驾
_MyAppState createState()=>\u MyAppState();
}
类MyAppState扩展了状态{
@凌驾
小部件构建(构建上下文){
回归未来建设者(
future:TokenStorageService.jwtOrEmpty,
生成器:(BuildContext上下文,异步快照){
var路径;
var initialRoute='/';
if(snapshot.hasData){
initialRoute='/';
打印(“用户存在”);
//TokenStorageService.clearStorage();
//TODO:检查是否过期
路线={
“/”:(上下文)=>HomePage(),
Profile.id:(上下文)=>Profile(),
AuthScreen.id:(上下文)=>AuthScreen(),
“/userLeave”:(上下文)=>userLeave(),
//构建UserLeave小部件。
“/leaveRequest”:(上下文)=>leaveRequest(),
//构建LeaveRequest小部件。
“/ownLeave”:(上下文)=>ownLeave(),
//构建LeaveRequest小部件。
“/AllLeave”:(上下文)=>AllLeave(),
//构建LeaveRequest小部件。
//构建注册小部件。
};
}否则{
initialRoute=AuthScreen.id;
打印(“未找到用户”);
路线={
“/”:(上下文)=>HomePage(),
//构建主页小部件。
AuthScreen.id:(上下文)=>AuthScreen(),
//
LoginScreen.id:(上下文)=>LoginScreen(),
//构建登录小部件。
RegistrationScreen.id:(上下文)=>RegistrationScreen(),
//构建LeaveRequest小部件。
};
///回到这里
}
返回材料PP(
标题:"路线",,
主题:主题数据(
脚手架背景颜色:颜色(0xFFF1F1),
),
初始路线:
initialRoute,//使用“/”命名路由启动应用程序。
路线:路线;;
//打印(初始路径);
//印刷(路线);
//TokenStorageService.clearStorage();
});
}
}

FutureBuilder只运行一次,因此一旦在FutureBuilder中获得数据,您将无法重新加载,因此请使用StreamBuilder。
class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return FutureBuilder<String>(
        future: TokenStorageService.jwtOrEmpty,
        builder: (BuildContext context, AsyncSnapshot<String> snapshot) {
          var routes;
          var initialRoute = '/';
          if (snapshot.hasData) {
            initialRoute = '/';
            print("user exist");
            // TokenStorageService.clearStorage();
            //TODO: check if expired

            routes = {
              '/': (context) => HomePage(),

              Profile.id: (context) => Profile(),

              AuthScreen.id: (context) => AuthScreen(),

              '/userLeave': (context) => UserLeave(),
              //  build the UserLeave widget.
              '/leaveRequest': (context) => LeaveRequest(),
              //  build the LeaveRequest widget.
              '/ownLeave': (context) => OwnLeave(),
              //  build the LeaveRequest widget.
              '/allLeaves': (context) => AllLeave(),
              //  build the LeaveRequest widget.
              // build the Register widget.
            };
          } else {
            initialRoute = AuthScreen.id;
            print("user not found");
            routes = {
              '/': (context) => HomePage(),
              // build the HomePage widget.
              AuthScreen.id: (context) => AuthScreen(),
              //
              LoginScreen.id: (context) => LoginScreen(),
              // build the Login widget.
              RegistrationScreen.id: (context) => RegistrationScreen(),
              //  build the LeaveRequest widget.
            };

            ///return here
          }

          return MaterialApp(
              title: 'routes',
              theme: ThemeData(
                scaffoldBackgroundColor: Color(0xFFF1F1F1),
              ),
              initialRoute:
                  initialRoute, // Start the app with the "/" named route.
              routes: routes);

          // print(initialRoute);
          // print(routes);
          // TokenStorageService.clearStorage();
        });
  }
}