【问题标题】:How to run other methods rather than runApp in flutter如何在flutter中运行其他方法而不是runApp
【发布时间】:2021-03-01 10:45:23
【问题描述】:

首先在主函数中,我想从存储中获取数据并检查它们,以便在用户未登录时阻止一些路由

我使用 JWT 和一些 AuthData 来检查应用程序运行时令牌是否过期

void main() => {
var authData = await TokenStorageService.authDataOrEmpty;
       runApp(
        MaterialApp(
          title: 'routes',
          theme: ThemeData(
            scaffoldBackgroundColor: Color(0xFFF1F1F1),
          ),
          initialRoute: '/', // Start the app with the "/" named route.
          routes: {
            '/': (context) => HomePage(),
// build the HomePage widget.
            WelcomeScreen.id: (context) => WelcomeScreen(),

            Profile.id: (context) => Profile(),
//build welcome screen
            LoginScreen.id: (context) => LoginScreen(),
// build the Login widget.
            RegistrationScreen.id: (context) => RegistrationScreen(),
// build the Register widget.
            '/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.
          },
        ),
      )
    };

【问题讨论】:

    标签: flutter dart flutter-dependencies


    【解决方案1】:

    你可以将 main 方法标记为异步,你很高兴

    Future<void> main() async {
      var authData = await TokenStorageService.authDataOrEmpty;
      // your other code
    }
    

    【讨论】:

    • 我想通了,我现在正在使用未来构建器中的代码
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-04
    • 2016-07-18
    • 1970-01-01
    • 1970-01-01
    • 2011-10-27
    • 2023-03-29
    相关资源
    最近更新 更多