How to use setState when call openDrawer?












0















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:



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:



          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!!










share|improve this question



























    0















    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:



    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:



              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!!










    share|improve this question

























      0












      0








      0








      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:



      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:



                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!!










      share|improve this question














      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:



      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:



                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!!







      flutter drawer






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 24 '18 at 3:58









      WinsonWinson

      261210




      261210
























          1 Answer
          1






          active

          oldest

          votes


















          0














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





          share|improve this answer


























          • sorry, can you explain how's your code work? I just use another way for solved my issue, thanks!

            – Winson
            Dec 17 '18 at 8:44











          • How to use the parameter "mainContext" directly? The context does not know that there is a Scaffold control. That's why WidgetBuilder is used, and you won't know the Scaffold control until "statelesswidget.build or state.build." is completed。

            – 陈英有
            Dec 18 '18 at 9:11











          • yes, so I use a GlobalKey for that, and change the async method to sync

            – Winson
            Dec 18 '18 at 12:33











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53455036%2fhow-to-use-setstate-when-call-opendrawer%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














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





          share|improve this answer


























          • sorry, can you explain how's your code work? I just use another way for solved my issue, thanks!

            – Winson
            Dec 17 '18 at 8:44











          • How to use the parameter "mainContext" directly? The context does not know that there is a Scaffold control. That's why WidgetBuilder is used, and you won't know the Scaffold control until "statelesswidget.build or state.build." is completed。

            – 陈英有
            Dec 18 '18 at 9:11











          • yes, so I use a GlobalKey for that, and change the async method to sync

            – Winson
            Dec 18 '18 at 12:33
















          0














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





          share|improve this answer


























          • sorry, can you explain how's your code work? I just use another way for solved my issue, thanks!

            – Winson
            Dec 17 '18 at 8:44











          • How to use the parameter "mainContext" directly? The context does not know that there is a Scaffold control. That's why WidgetBuilder is used, and you won't know the Scaffold control until "statelesswidget.build or state.build." is completed。

            – 陈英有
            Dec 18 '18 at 9:11











          • yes, so I use a GlobalKey for that, and change the async method to sync

            – Winson
            Dec 18 '18 at 12:33














          0












          0








          0







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





          share|improve this answer















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






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Dec 7 '18 at 3:54









          TeeKea

          3,20441630




          3,20441630










          answered Dec 7 '18 at 3:11









          陈英有陈英有

          1




          1













          • sorry, can you explain how's your code work? I just use another way for solved my issue, thanks!

            – Winson
            Dec 17 '18 at 8:44











          • How to use the parameter "mainContext" directly? The context does not know that there is a Scaffold control. That's why WidgetBuilder is used, and you won't know the Scaffold control until "statelesswidget.build or state.build." is completed。

            – 陈英有
            Dec 18 '18 at 9:11











          • yes, so I use a GlobalKey for that, and change the async method to sync

            – Winson
            Dec 18 '18 at 12:33



















          • sorry, can you explain how's your code work? I just use another way for solved my issue, thanks!

            – Winson
            Dec 17 '18 at 8:44











          • How to use the parameter "mainContext" directly? The context does not know that there is a Scaffold control. That's why WidgetBuilder is used, and you won't know the Scaffold control until "statelesswidget.build or state.build." is completed。

            – 陈英有
            Dec 18 '18 at 9:11











          • yes, so I use a GlobalKey for that, and change the async method to sync

            – Winson
            Dec 18 '18 at 12:33

















          sorry, can you explain how's your code work? I just use another way for solved my issue, thanks!

          – Winson
          Dec 17 '18 at 8:44





          sorry, can you explain how's your code work? I just use another way for solved my issue, thanks!

          – Winson
          Dec 17 '18 at 8:44













          How to use the parameter "mainContext" directly? The context does not know that there is a Scaffold control. That's why WidgetBuilder is used, and you won't know the Scaffold control until "statelesswidget.build or state.build." is completed。

          – 陈英有
          Dec 18 '18 at 9:11





          How to use the parameter "mainContext" directly? The context does not know that there is a Scaffold control. That's why WidgetBuilder is used, and you won't know the Scaffold control until "statelesswidget.build or state.build." is completed。

          – 陈英有
          Dec 18 '18 at 9:11













          yes, so I use a GlobalKey for that, and change the async method to sync

          – Winson
          Dec 18 '18 at 12:33





          yes, so I use a GlobalKey for that, and change the async method to sync

          – Winson
          Dec 18 '18 at 12:33


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53455036%2fhow-to-use-setstate-when-call-opendrawer%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown