setState() or markNeedsBuild() called > when widget tree was locked. when changing state2019 Community Moderator ElectionFlutter setState() or markNeedsBuild() called when widget tree was lockedFlutter - Implementing a Navigation drawer with a TabBarView widget with dynamic Tab viewsetState() or markNeedsBuild called during buildHow to offset a scaffold widget in Flutter?Flutter: setState() or markNeedsBuild() called when widget tree was locked… during orientation changeFlutter : Bad state: Stream has already been listened toText widget not displaying in ListTile flutterI got stuck with a problem in Flutter: The following assertion was thrown building TextField, it flesh me a stranger problemsetstate vs normal assignment in a stateful widgetpropagate state of stateful widget to its child widget

How do I deal with a powergamer in a game full of beginners in a school club?

Solving "Resistance between two nodes on a grid" problem in Mathematica

Make a transparent 448*448 image

Do Bugbears' arms literally get longer when it's their turn?

PTIJ: Why can't I eat anything?

infinitive telling the purpose

Unreachable code, but reachable with exception

Offered promotion but I'm leaving. Should I tell?

They call me Inspector Morse

Peter's Strange Word

Good for you! in Russian

Should I tell my boss the work he did was worthless

Why would a jet engine that runs at temps excess of 2000°C burn when it crashes?

Algorithm to convert a fixed-length string to the smallest possible collision-free representation?

How does airport security verify that you can carry a battery bank over 100 Wh?

A three room house but a three headED dog

Is Gradient Descent central to every optimizer?

What is the chance of making a successful appeal to dismissal decision from a PhD program after failing the qualifying exam in the 2nd attempt?

How are such low op-amp input currents possible?

Could you please stop shuffling the deck and play already?

Grey hair or white hair

Are there historical instances of the capital of a colonising country being temporarily or permanently shifted to one of its colonies?

Accountant/ lawyer will not return my call

Rejected in 4th interview round citing insufficient years of experience



setState() or markNeedsBuild() called > when widget tree was locked. when changing state



2019 Community Moderator ElectionFlutter setState() or markNeedsBuild() called when widget tree was lockedFlutter - Implementing a Navigation drawer with a TabBarView widget with dynamic Tab viewsetState() or markNeedsBuild called during buildHow to offset a scaffold widget in Flutter?Flutter: setState() or markNeedsBuild() called when widget tree was locked… during orientation changeFlutter : Bad state: Stream has already been listened toText widget not displaying in ListTile flutterI got stuck with a problem in Flutter: The following assertion was thrown building TextField, it flesh me a stranger problemsetstate vs normal assignment in a stateful widgetpropagate state of stateful widget to its child widget










1















Please look at my code:



class HomePageState extends State<HomePage> 
bool _isLoading = false;

.....

@override
Widget build(BuildContext context)

var drawerOptions = <Widget>[];
......

drawerOptions.add(new ListTile(
leading: new Icon(Icons.account_balance),
title: new Text(Strings.menu_change_city),
onTap: () => createDialog()
));


if(_isLoading) return buildBusyForm();

return Scaffold( .... //window content





So I have navigation drawer. One item ("Select city") does not close navigation drawer, it shows select city dialog:



createDialog() 

setState(() _isLoading = true;);

fetchCities().then((response)

setState(() _isLoading = false;);

showDialog(
context: context,
builder: (context) => CityChoiceDialog<City>(
title: Text(Strings.menu_change_city),
items: response,
initialValue: response.firstWhere((c) => c.id == globals.cityId, orElse: () => new City()),
itemBuilder: (City city) => Text(city.name),
onSelected: _onSelected,
onSubmitted: _onSubmitted));
);



So, basically it's intended to show busy form, load cities, then hide busy form and show cities list dialog. As it seems to work, I'm getting exception:




I/flutter (10662): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY
╞═══════════════════════════════════════════════════════════ I/flutter
(10662): The following assertion was thrown while finalizing the
widget tree: I/flutter (10662): setState() or markNeedsBuild() called
when widget tree was locked. I/flutter (10662): This
_ModalScope widget cannot be marked as needing to build because the framework is I/flutter (10662): locked. I/flutter (10662):
The widget on which setState() or markNeedsBuild() was called was:
I/flutter (10662):

_ModalScope-[LabeledGlobalKey<_ModalScopeState>#1f222](state:
I/flutter (10662): _ModalScopeState#6c40b)




How to do what I want correctly?










share|improve this question
























  • how exactly are you closing your drawer ?

    – Mazin Ibrahim
    Mar 7 at 8:27











  • I noticed problem can be solved if I close drawer before attempting to change state. If drawer is still open and I want to change state, I'm getting this exception.

    – user1209216
    Mar 7 at 12:15











  • this kind of exception is noticed in the debugger only, but when you run this app on an actual device it won't have any noticeable effects.I tried that.

    – Mazin Ibrahim
    Mar 7 at 12:19
















1















Please look at my code:



class HomePageState extends State<HomePage> 
bool _isLoading = false;

.....

@override
Widget build(BuildContext context)

var drawerOptions = <Widget>[];
......

drawerOptions.add(new ListTile(
leading: new Icon(Icons.account_balance),
title: new Text(Strings.menu_change_city),
onTap: () => createDialog()
));


if(_isLoading) return buildBusyForm();

return Scaffold( .... //window content





So I have navigation drawer. One item ("Select city") does not close navigation drawer, it shows select city dialog:



createDialog() 

setState(() _isLoading = true;);

fetchCities().then((response)

setState(() _isLoading = false;);

showDialog(
context: context,
builder: (context) => CityChoiceDialog<City>(
title: Text(Strings.menu_change_city),
items: response,
initialValue: response.firstWhere((c) => c.id == globals.cityId, orElse: () => new City()),
itemBuilder: (City city) => Text(city.name),
onSelected: _onSelected,
onSubmitted: _onSubmitted));
);



So, basically it's intended to show busy form, load cities, then hide busy form and show cities list dialog. As it seems to work, I'm getting exception:




I/flutter (10662): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY
╞═══════════════════════════════════════════════════════════ I/flutter
(10662): The following assertion was thrown while finalizing the
widget tree: I/flutter (10662): setState() or markNeedsBuild() called
when widget tree was locked. I/flutter (10662): This
_ModalScope widget cannot be marked as needing to build because the framework is I/flutter (10662): locked. I/flutter (10662):
The widget on which setState() or markNeedsBuild() was called was:
I/flutter (10662):

_ModalScope-[LabeledGlobalKey<_ModalScopeState>#1f222](state:
I/flutter (10662): _ModalScopeState#6c40b)




How to do what I want correctly?










share|improve this question
























  • how exactly are you closing your drawer ?

    – Mazin Ibrahim
    Mar 7 at 8:27











  • I noticed problem can be solved if I close drawer before attempting to change state. If drawer is still open and I want to change state, I'm getting this exception.

    – user1209216
    Mar 7 at 12:15











  • this kind of exception is noticed in the debugger only, but when you run this app on an actual device it won't have any noticeable effects.I tried that.

    – Mazin Ibrahim
    Mar 7 at 12:19














1












1








1








Please look at my code:



class HomePageState extends State<HomePage> 
bool _isLoading = false;

.....

@override
Widget build(BuildContext context)

var drawerOptions = <Widget>[];
......

drawerOptions.add(new ListTile(
leading: new Icon(Icons.account_balance),
title: new Text(Strings.menu_change_city),
onTap: () => createDialog()
));


if(_isLoading) return buildBusyForm();

return Scaffold( .... //window content





So I have navigation drawer. One item ("Select city") does not close navigation drawer, it shows select city dialog:



createDialog() 

setState(() _isLoading = true;);

fetchCities().then((response)

setState(() _isLoading = false;);

showDialog(
context: context,
builder: (context) => CityChoiceDialog<City>(
title: Text(Strings.menu_change_city),
items: response,
initialValue: response.firstWhere((c) => c.id == globals.cityId, orElse: () => new City()),
itemBuilder: (City city) => Text(city.name),
onSelected: _onSelected,
onSubmitted: _onSubmitted));
);



So, basically it's intended to show busy form, load cities, then hide busy form and show cities list dialog. As it seems to work, I'm getting exception:




I/flutter (10662): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY
╞═══════════════════════════════════════════════════════════ I/flutter
(10662): The following assertion was thrown while finalizing the
widget tree: I/flutter (10662): setState() or markNeedsBuild() called
when widget tree was locked. I/flutter (10662): This
_ModalScope widget cannot be marked as needing to build because the framework is I/flutter (10662): locked. I/flutter (10662):
The widget on which setState() or markNeedsBuild() was called was:
I/flutter (10662):

_ModalScope-[LabeledGlobalKey<_ModalScopeState>#1f222](state:
I/flutter (10662): _ModalScopeState#6c40b)




How to do what I want correctly?










share|improve this question
















Please look at my code:



class HomePageState extends State<HomePage> 
bool _isLoading = false;

.....

@override
Widget build(BuildContext context)

var drawerOptions = <Widget>[];
......

drawerOptions.add(new ListTile(
leading: new Icon(Icons.account_balance),
title: new Text(Strings.menu_change_city),
onTap: () => createDialog()
));


if(_isLoading) return buildBusyForm();

return Scaffold( .... //window content





So I have navigation drawer. One item ("Select city") does not close navigation drawer, it shows select city dialog:



createDialog() 

setState(() _isLoading = true;);

fetchCities().then((response)

setState(() _isLoading = false;);

showDialog(
context: context,
builder: (context) => CityChoiceDialog<City>(
title: Text(Strings.menu_change_city),
items: response,
initialValue: response.firstWhere((c) => c.id == globals.cityId, orElse: () => new City()),
itemBuilder: (City city) => Text(city.name),
onSelected: _onSelected,
onSubmitted: _onSubmitted));
);



So, basically it's intended to show busy form, load cities, then hide busy form and show cities list dialog. As it seems to work, I'm getting exception:




I/flutter (10662): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY
╞═══════════════════════════════════════════════════════════ I/flutter
(10662): The following assertion was thrown while finalizing the
widget tree: I/flutter (10662): setState() or markNeedsBuild() called
when widget tree was locked. I/flutter (10662): This
_ModalScope widget cannot be marked as needing to build because the framework is I/flutter (10662): locked. I/flutter (10662):
The widget on which setState() or markNeedsBuild() was called was:
I/flutter (10662):

_ModalScope-[LabeledGlobalKey<_ModalScopeState>#1f222](state:
I/flutter (10662): _ModalScopeState#6c40b)




How to do what I want correctly?







exception dart flutter state






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 7 at 12:55







user1209216

















asked Mar 7 at 8:00









user1209216user1209216

1,83042363




1,83042363












  • how exactly are you closing your drawer ?

    – Mazin Ibrahim
    Mar 7 at 8:27











  • I noticed problem can be solved if I close drawer before attempting to change state. If drawer is still open and I want to change state, I'm getting this exception.

    – user1209216
    Mar 7 at 12:15











  • this kind of exception is noticed in the debugger only, but when you run this app on an actual device it won't have any noticeable effects.I tried that.

    – Mazin Ibrahim
    Mar 7 at 12:19


















  • how exactly are you closing your drawer ?

    – Mazin Ibrahim
    Mar 7 at 8:27











  • I noticed problem can be solved if I close drawer before attempting to change state. If drawer is still open and I want to change state, I'm getting this exception.

    – user1209216
    Mar 7 at 12:15











  • this kind of exception is noticed in the debugger only, but when you run this app on an actual device it won't have any noticeable effects.I tried that.

    – Mazin Ibrahim
    Mar 7 at 12:19

















how exactly are you closing your drawer ?

– Mazin Ibrahim
Mar 7 at 8:27





how exactly are you closing your drawer ?

– Mazin Ibrahim
Mar 7 at 8:27













I noticed problem can be solved if I close drawer before attempting to change state. If drawer is still open and I want to change state, I'm getting this exception.

– user1209216
Mar 7 at 12:15





I noticed problem can be solved if I close drawer before attempting to change state. If drawer is still open and I want to change state, I'm getting this exception.

– user1209216
Mar 7 at 12:15













this kind of exception is noticed in the debugger only, but when you run this app on an actual device it won't have any noticeable effects.I tried that.

– Mazin Ibrahim
Mar 7 at 12:19






this kind of exception is noticed in the debugger only, but when you run this app on an actual device it won't have any noticeable effects.I tried that.

– Mazin Ibrahim
Mar 7 at 12:19













1 Answer
1






active

oldest

votes


















0














This error means you are calling setState during the build phase, which you cannot do.






share|improve this answer






















    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%2f55038765%2fsetstate-or-markneedsbuild-called-when-widget-tree-was-locked-when-changi%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














    This error means you are calling setState during the build phase, which you cannot do.






    share|improve this answer



























      0














      This error means you are calling setState during the build phase, which you cannot do.






      share|improve this answer

























        0












        0








        0







        This error means you are calling setState during the build phase, which you cannot do.






        share|improve this answer













        This error means you are calling setState during the build phase, which you cannot do.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 7 at 8:16









        Hussein AbdallahHussein Abdallah

        1647




        1647





























            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%2f55038765%2fsetstate-or-markneedsbuild-called-when-widget-tree-was-locked-when-changi%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







            Popular posts from this blog

            Thal And Out Agency railway station See also References External links Navigation menuOfficial Web Site of Pakistan RailwaysArchivedOfficial Web Site of Pakistan Railwayseeexpanding ite

            Posting a File and Associated Data to a RESTful WebService preferably as JSON2019 Community Moderator ElectionREST URI convention - Singular or plural name of resource while creating itUploading both data and files in one form using Ajax?How do I upload a file with metadata using a REST web service?REST API - file (ie images) processing - best practicesSpring: JSON data and file in the same requestREST API Put large dataPosting a file and JSON data to Spring rest serviceRESTful Web Service Upload/Download Large Data With JSONREST API Design sending JSON data and a file to the api in same requestPost JSONArray to REST servicePUT vs. POST in RESTParsing values from a JSON file?JavaScript/jQuery to download file via POST with JSON dataHow do I upload a file with metadata using a REST web service?How to POST JSON data with Curl from Terminal/Commandline to Test Spring REST?Separate REST JSON API server and client?what's the correct way to send a file from REST web service to client?How do I write JSON data to a file?What is “406-Not Acceptable Response” in HTTP?REST API - file (ie images) processing - best practices

            How can I change the color of pagination dots of UIPageControl?How to change UIPageControl dotsIs there a way to change page indicator dots colorCustomize dot with image of UIPageControl at index 0 of UIPageControlNo visible @interface for 'NSObject<PageControlDelegate>' declares the selector 'pageControlPageDidChange:'How to change the color of pagination dots in UIPageControl with a different color per pagepagecontrol indicator custom image instead of DefaultChanging the colour of UIPageControl dots in MonoTouchpagecontrol selectable page visibility color?Alternative way to load ViewControllers on a UIPageControlHow to set only layer.border-color for UIpage control dots in swiftHow can I develop for iPhone using a Windows development machine?How to change the name of an iOS app?UITableView - change section header coloruipagecontrol indicator(dot)issueCustom UIPageControl dots color not changingchange the interspace between UIPageControl dotsHow to change Status Bar text color in iOSHow can I change image tintColor in iOS and WatchKitUIPageControl dots with larger space in between each dotsHow to change the color of pagination dots in UIPageControl with a different color per page