简体   繁体   English

调用openDrawer时如何使用setState?

[英]How to use setState when call openDrawer?

In my drawer, there are two actions, if user logined, it will show the user information, otherwise will show popup the login screen, so I want to call the setState when I open the drawer as below: 在我的抽屉中,有两个操作,如果用户登录,它将显示用户信息,否则将弹出登录屏幕,因此我想在打开抽屉时调用setState,如下所示:

clickLeftMenu: (BuildContext mainContext) {
      LoginInfo.checkLogin(mainContext).then((islogin) {
        print(islogin.toString());
        if (!islogin)
          showLogin(mainContext, true);
        else{
          Scaffold.of(mainContext).openDrawer();
          setState(() {
            //I want to update the user login info for rebuild screen for show
            loginInfo = LoginInfo.getInfo();
          });
        }              
      });
    },

show the user info with loginInfo object in drawer: 在抽屉中使用loginInfo对象显示用户信息:

          ListTile(
            leading: Icon(Icons.account_circle,
                color: Theme.of(context).primaryColor,
                size: setWidth(context, 8.0)),
            title: Text(
              loginInfo.name ?? '',
              //loginInfo. != null ? loginInfo.email : '',
              style: TextStyle(
                fontSize: 20.0,
                color: Theme.of(context).primaryColor,
              ),
            ),
            subtitle: Text(
              loginInfo.email ?? '',
              style: TextStyle(
                fontSize: 10.0,
                //color: this.primaryColor,
              ),
            ),
          ),

but it failed when I called that, show below error: 但是当我打电话时失败了,显示以下错误:

setState() or markNeedsBuild() called when widget tree was locked.
This _ModalScope<dynamic> widget cannot be marked as needing to build because the framework is locked

I tried many of ways but still failed, do you have any ideas or other ways can do that? 我尝试了很多方法,但还是失败了,您有任何想法或其他方法可以做到吗?

Thanks!! 谢谢!!

 Builder(
        builder: (BuildContext context) {
          return IconButton(
            icon: const Icon(Icons.menu),
            onPressed: () {
              Scaffold.of(context).openDrawer();
            },
            tooltip: MaterialLocalizations.of(context).openAppDrawerTooltip,
          );
        },
      ), 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM