Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

Flutter : setState not working, Unhandled Exception: setState() called after dispose(): FormState#416d6(lifecycle state: defunct, not mounted)

The full error is :

Unhandled Exception: setState() called after dispose(): FormState#416d6(lifecycle state: defunct, not mounted)
E/flutter ( 4898): This error happens if you call setState() on a State object for a widget that no longer appears in the widget tree (e.g., whose parent widget no longer includes the widget in its build). This error can occur when code calls setState() from a timer or an animation callback.

Looking at other answers on Stack, I added a check if(!mounted)return; but it didn't do anything.

My code where I am having issue is :

                            FlatButton(
                                  onPressed: () async {
                                    if (!mounted) return;
                                    setState(() {
                                      isLoading = true;
                                    });
                                   await saveSignUpData();

                                    Navigator.push(
                                        context,
                                        MaterialPageRoute(
                                            builder: (context) => Home()));
                                    isLoading = false;
                                  },
                                  child: Center(
                                    child: Text("Continue",
                                        style:
                                            Theme.of(context).textTheme.button),
                                  ),
                                ),

If I remove setState, my code is working fine but with setState I am getting the aforementioned error and this is the only place where I am using setState.

saveSignUpData function is just signing up the user with the help of NodeJS.

question from:https://stackoverflow.com/questions/65925502/flutter-setstate-not-working-unhandled-exception-setstate-called-after-dis

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...