zoukankan      html  css  js  c++  java
  • How can I call an async method in StatelessWidget.build method?

    I have below code in flutter. In initialRoute: attribute, it needs to call isLoggedIn() method which is an async function. I got an error saying I need to call await in a async function. But the build method is overridden from its parent class which is not async method. How can I call an await inside an overridden method?

    class MyApp extends StatelessWidget {
      // This widget is the root of your application.
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: '',
          theme: ThemeData(
            primarySwatch: Colors.blue,
          ),
          initialRoute: await isLoggedIn() ? '/': '/login',
          routes: <String, WidgetBuilder>{
            '/': (BuildContext context) {
              return MyHomePage(title: 'Home Page');
            },
            '/login': (BuildContext context) {
              return Login();
            },
          }
    
        );
      }
    }
    

      

    answer 1;

    According to me it is impossible to do what you are trying to do.

    As you mention isLoggedIn is async which is gonna take time to calculate, meanwhile build method complete build. You can use futureBuilder to do async task in Stateless widget but as this is your initial route, you have to provide it, so again it will not work.

    I recommend you to add splash screen, where you can calculate mean call that method and find out navigation.

    answer 2;

    I would suggest it would be a better option to create an authentication router widget to handle the logged in status of the app that way if the user logs out of the app the navigation can change automatically. i.e:

    initialRoute: '/user-auth'
    

    Then is a UserAuth widget you can run your logged in logic.

    answer 3;, ---ok

    When I had this problem, I solved it by putting the await not inside MyApp.build, but in the MyApp call in main. Like this:

    Future <Widget> selectStartPage () async {
        return await isLoggedIn() ? HomePage (): Login (),
    }
    
    Future<void> main() async {
       // whatever setup you need...
       runApp (MyApp (await selectStartPage()));
    }
    
    class MyApp extends StatelessWidget {
       final Widget startPage;
       MyApp (this.startPage);
    
       @override Widget build (BuildContext context){
          return MaterialApp(
             title: '',
             theme: ThemeData(
                primarySwatch: Colors.blue,
             ),
             initialRoute: startPage,
          );
       }
    }
    

      

    Futurebuilder in statelesswidget.............

    Getx onInit..

  • 相关阅读:
    软件开发的升级打怪攻略:从新手到高级工程师
    Java实现递归将嵌套Map里的字段名由驼峰转为下划线
    生活是什么
    批量下载网站图片的Python实用小工具
    工作的方法
    工作的心境
    LODOP直接导出图片不弹框
    LODOP打印table超宽用省略号带'-'的内容换行问题
    LODOP打印table表格宽度固定-超宽隐藏
    如何领购和作废电子发票流程
  • 原文地址:https://www.cnblogs.com/pythonClub/p/15782271.html
Copyright © 2011-2022 走看看