4

I have simple Flutter app with list of items that are loaded from Firebase database (Cloud Firestore).

enter image description here

As you can see - there is button for adding items and each item can be deleted or edited. When I press edit button for selected item, AlertDialog with TextField appears, in this TextField user can see current item name and edit it. I have problems only with dismissing dialog after editing.

   new IconButton(
      icon: new Icon(Icons.edit, color: Colors.white),
      onPressed: (){ showItemUpdateDialog(context, document); }
   )
   .......


void showItemUpdateDialog(BuildContext context, DocumentSnapshot item) {

  String itemName = "";
  var textContoller = new TextEditingController();
  textContoller.text = item['name'];

  var dialog = new AlertDialog(
    title: new Text("item name"),
    content: new TextField(
      controller: textContoller,
      onChanged: (value) {newName = value;},
    ),
    actions: <Widget>[
      new FlatButton(
        child: Text("cancel"),
        onPressed: (){
          Navigator.pop(context);
        },
      ),
      new FlatButton(
          child: Text("Update"),
          onPressed: () { 
            updateItemOnServer(item, newName); 
            Navigator.pop(context); 
          }
      )
    ],
  );

  showDialog(context: context, child: dialog);
}

Value is updating correctly but AlertDialog is not dismissed. Error code is below. I think that it is due to that is was called by item that was modified and updated from server.

flutter: The following assertion was thrown while handling a gesture: flutter: Looking up a deactivated widget's ancestor is unsafe. flutter: At this point the state of the widget's element tree is no longer stable. To safely refer to a flutter: widget's ancestor in its dispose() method, save a reference to the ancestor by calling flutter: inheritFromWidgetOfExactType() in the widget's didChangeDependencies() method.

2
  • Did you resolve this? If so, how? Commented Oct 11, 2018 at 19:17
  • Hello @moonvader Can you Please Help Me Out i want to implement same functionalities like you here is My question : stackoverflow.com/questions/61092208/… Commented Apr 8, 2020 at 2:08

3 Answers 3

14

Try this,

Navigator.of(context, rootNavigator: true).pop();
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for this! I had multiple navigators in my app and I couldn't figure out why none of the other answers would work for me.
@shanmugavel-gk can you please explain why does this work?
From the docs: If rootNavigator is set to true, the state from the furthest instance of this class is given instead. Useful for pushing contents above all subsequent instances of Navigator.
7

With the latest Flutter use:

Navigator.of(context).pop();

instead of

Navigator.pop(context);

For some reason it pops twice from the stack when popping Dialog

Do let me know if this solves the problem!

Comments

2

Try This,

Navigator.popUntil(context, ModalRoute.withName('/login'));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.