0

I keep getting an Error shown below. This error happens when I click a button to open my dialog, the moment I type anything in the form field and click the button, this error will show up. Please help!

"The following assertion was thrown while handling a gesture: Looking up a deactivated widget's ancestor is unsafe.

At this point the state of the widget's element tree is no longer stable.

To safely refer to a widget's ancestor in its dispose() method, save a reference to the ancestor by calling dependOnInheritedWidgetOfExactType() in the widget's didChangeDependencies() method."

void deleteDialog(BuildContext context) {
    final _pinController = TextEditingController();
    final _formKey = GlobalKey<FormState>();
    var alert = AlertDialog(
        title: Text('Delete Post'),
        content: Text('Are you sure you want to delete?'),
        actions: <Widget>[
          Form(
              key: _formKey,
              child: Column(mainAxisSize: MainAxisSize.min, children: <Widget>[
                Padding(
                    padding: const EdgeInsets.fromLTRB(10.0, 0.0, 10.0, 20.0),
                    child: TextFormField(
                      obscureText: true,
                      decoration: InputDecoration(
                        border: OutlineInputBorder(
                          borderRadius: const BorderRadius.all(
                            const Radius.circular(25),
                          ),
                        ),
                        fillColor: secondaryTheme,
                        labelText: 'Admin PIN',
                      ),
                      validator: (val) =>
                          val!.isEmpty ? 'Enter your Admin PIN' : null,
                      controller: _pinController,
                    )),
                Row(
                    mainAxisAlignment: MainAxisAlignment.end,
                    children: <Widget>[
                      ElevatedButton(
                          style: ElevatedButton.styleFrom(
                            primary: secondaryTheme,
                          ),
                          child: Text('Cancel'),
                          onPressed: () {
                            Navigator.of(context).pop();
                          }),
                      SizedBox(width: 20),
                      ElevatedButton(
                          style: ElevatedButton.styleFrom(
                            primary: mainTheme,
                          ),
                          child: Text('Delete'),
                          onPressed: () {
                            if (_formKey.currentState!.validate()) {
                              verifyPIN(developmentDomainRecord.id.toString(),
                                  _pinController.text, context);
                            }
                          }),
                    ])
              ]))
        ]);

    showDialog(
        context: context,
        builder: (BuildContext context) {
          return alert;
        });
  }

void verifyPIN(String key, String pin, BuildContext context) async {
    SharedPreferences prefs = await SharedPreferences.getInstance();

    var adminPin = prefs.getString('adminPin').toString();
    var pinBytes = utf8.encode(pin); // data being hashed
    var hashedPin = sha256.convert(pinBytes).toString(); // Hashing Process

    if (adminPin == hashedPin) {
      FirebaseFirestore.instance
          .collection('developmentdomain')
          .doc(key)
          .delete();

      Navigator.of(context).pop();
    } else {
      Fluttertoast.showToast(
          msg: "Invalid Admin PIN",
          toastLength: Toast.LENGTH_SHORT,
          gravity: ToastGravity.CENTER,
          timeInSecForIosWeb: 1,
          backgroundColor: Colors.red,
          textColor: Colors.white,
          fontSize: 16.0);
      print("Invalid Admin PIN");
    }
  }
der
  • 39
  • 5
  • Have you checked similar threads about this error? You can use these past threads as a guide: [Flutter: Looking up a deactivated widget's ancestor is unsafe](https://stackoverflow.com/questions/61424636/flutter-looking-up-a-deactivated-widgets-ancestor-is-unsafe?rq=1), [Looking up a deactivated widget's ancestor is unsafe](https://stackoverflow.com/questions/54617432/looking-up-a-deactivated-widgets-ancestor-is-unsafe), [github thread](https://github.com/flutter/flutter/issues/62126) – Farid Shumbar Jul 23 '21 at 08:59

0 Answers0