title: flutter error: This widget has been unmounted, so the State no longer has a context (and should be considered defunct)
body: <p>when I clicking on login button when my mobile number is wrong then open one error dialog box after that when I clicked okey on dialog box then i got this error.</p>
  <p>I got this message</p>
  <blockquote>
  <p>This widget has been unmounted, so the State no longer has a context
  (and should be considered defunct)</p>
  <p>Consider canceling any active work during &quot;dispose&quot; or using the
  &quot;mounted&quot; getter to determine if the State is still active.</p>
  </blockquote>
  <pre><code>class _LoginPageState extends State&lt;LoginPage&gt; {
    final GlobalKey&lt;FormState&gt; _formKey = GlobalKey();
  
    TextEditingController _mobileController = new TextEditingController();
    // TextEditingController _nameController = new TextEditingController();
    // TextEditingController _emailController = new TextEditingController();
    TextEditingController _passwordController = new TextEditingController();
  
    Map&lt;String, String&gt; _authData = {
      'user_name': '',
      'user_email': '',
      'username': '',
      'password': ''
    };
  
    Future _submit() async {
      print('aa' + _authData['username']);
      if (!_formKey.currentState.validate()) {
     
  
        return;
      }
      _formKey.currentState.save();
      try {
        await Provider.of&lt;Auth&gt;(context, listen: false).login(
            _authData['user_name'],
            _authData['user_email'],
            _authData['username'],
            _authData['password']);
  
  
        print('authData_username' + _authData['username']);
        print('authData_password' + _authData['password']);
      } on HttpException catch (e) {
        // } catch (e) {
        var errorMessage = 'Authentication Failed';
        if (e.toString().contains('Entered wrong mobile number!')) {
          errorMessage = 'Entered wrong mobile number!';
          print(errorMessage);
          _showerrorDialog(errorMessage);
        } else if (e.toString().contains('Entered wrong password!')) {
          errorMessage = 'Entered wrong password!';
          _showerrorDialog(errorMessage);
        }
      } catch (error) {
        var errorMessage = 'Please try again later';
        _showerrorDialog(errorMessage);
      }
    }
  
    @override
    void dispose() {
      // TODO: implement dispose
      super.dispose();
    }
  
    @override
    Widget build(BuildContext context) {
      return Scaffold(
        body: Container(
          child: ListView(
            children: &lt;Widget&gt;[
              Container(
                width: MediaQuery.of(context).size.width,
                height: MediaQuery.of(context).size.height / 3.5,
                decoration: BoxDecoration(
                    gradient: LinearGradient(
                        begin: Alignment.topCenter,
                        end: Alignment.bottomCenter,
                        
                        colors: myGradient2),
                    borderRadius:
                        BorderRadius.only(bottomLeft: Radius.circular(90))),
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: &lt;Widget&gt;[
                    Spacer(),
                    Align(
                      alignment: Alignment.center,
                      child: Icon(
                        Icons.person,
                        size: 90,
                        color: kWhite,
                      ),
                    ),
                    Spacer(),
                    Align(
                      alignment: Alignment.bottomRight,
                      child: Padding(
                        padding: const EdgeInsets.only(bottom: 32, right: 32),
                        child: Text(
                          'Login',
                          style: TextStyle(color: kWhite, fontSize: 18),
                        ),
                      ),
                    ),
                  ],
                ),
              ),
              Container(
                height: MediaQuery.of(context).size.height / 2,
                width: MediaQuery.of(context).size.width,
                padding: EdgeInsets.only(top: 62),
                child: Form(
                  key: _formKey,
                  child: Column(
                    children: &lt;Widget&gt;[
                      Container(
                        width: MediaQuery.of(context).size.width / 1.2,
                        // height: 45,
                        child: TextFormField(
                          keyboardType: TextInputType.name,
                          autofocus: false,
                          decoration: InputDecoration(
                            hintText: 'Username.',
                            contentPadding:
                                EdgeInsets.fromLTRB(20.0, 15.0, 20.0, 15.0),
                            border: OutlineInputBorder(
                                borderRadius: BorderRadius.circular(32.0)),
                          ),
                          validator: (input) =&gt;
                              input.length &gt; 10 || input.length &lt; 10
                                  ? &quot;Mobile Number should be valid&quot;
                                  : null,
                          controller: _mobileController,
                          onSaved: (value) {
                            _authData['username'] = value;
                          },
                          // maxLength: 10,
                        ),
                      ),
                      SizedBox(
                        height: 10,
                      ),
                      Container(
                        width: MediaQuery.of(context).size.width / 1.2,
                        // height: 45,
                        child: TextFormField(
                          keyboardType: TextInputType.text,
                          autofocus: false,
                          obscureText: true,
                          decoration: InputDecoration(
                            hintText: 'Password',
                            contentPadding:
                                EdgeInsets.fromLTRB(20.0, 15.0, 20.0, 15.0),
                            border: OutlineInputBorder(
                                borderRadius: BorderRadius.circular(32.0)),
                          ),
                          validator: (input) =&gt; input.length &lt; 3
                              ? &quot;Password should be more than 3 characters&quot;
                              : null,
                          controller: _passwordController,
                          onSaved: (value) {
                            _authData['password'] = value;
                          },
                        ),
                      ),
                      Align(
                        alignment: Alignment.centerRight,
                        child: Padding(
                          padding: const EdgeInsets.only(top: 16, right: 32),
                          child: Text(
                            'Forgot Password ?',
                            style: TextStyle(color: Colors.grey),
                          ),
                        ),
                      ),
                      Spacer(),
                      InkWell(
                        onTap: () {
                          _submit();
                        },
                        child: Container(
                          height: 45,
                          width: MediaQuery.of(context).size.width / 1.2,
                          decoration: BoxDecoration(
                              gradient: LinearGradient(
                                colors: myGradient2,
                              ),
                              borderRadius:
                                  BorderRadius.all(Radius.circular(50))),
                          child: Center(
                            child: Text(
                              'Login'.toUpperCase(),
                              style: TextStyle(
                                  color: Colors.white,
                                  fontWeight: FontWeight.bold),
                            ),
                          ),
                        ),
                      ),
                    ],
                  ),
                ),
              ),
              SizedBox(
                height: 50,
              ),
              InkWell(
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: &lt;Widget&gt;[
                    Text(&quot;Dnon't have an account ?&quot;),
                    Text(
                      &quot;Sign Up&quot;,
                      style: TextStyle(color: kGreen),
                    ),
                  ],
                ),
                onTap: () {
                  // Navigator.pushNamed(context, '/signup');
                  Navigator.push(
                    context,
                    MaterialPageRoute(builder: (context) =&gt; SignupPage()),
                  );
                },
              ),
            ],
          ),
        ),
      );
    }
  
    void _showerrorDialog(String message) {
      showDialog(
        context: context,
        builder: (ctx) =&gt; AlertDialog(
          title: Text(
            'An Error Occurs',
            style: TextStyle(color: Colors.blue),
          ),
          content: Text(message),
          actions: &lt;Widget&gt;[
            FlatButton(
                child: Text('Okay'), onPressed: () =&gt; Navigator.of(mounted).pop())
          ],
        ),
      );
    }
  }
  
  </code></pre>