I have an order-page in my app where the user can remove the order. When the button below is pressed the order gets removed and the screen gets poped. But in that short time before the screen is poped an error and a red screen shows for half a second because the order that is recuired not exist anymore, but as the pop is completed the app works fine. How should i solve this? I know I could use a diffrent approach where the order isnt removed from its list but i want to make it work this way and I want to pop the screen before i remove the order from the list.

The error message: ════════ Exception caught by widgets library ═══════════════════════════════════ Bad state: No element

ElevatedButton(
                  child: Text('Remove'),
                  onPressed: () {
                    Navigator.pop(context);
                    Provider.of<Orders>(context, listen: false)
                        .removeOrder(order.id);

                  },
                ),


Solution 1: Usama majid

onPressed: () {
Provider.of<Orders>(context, listen: false)
.removeOrder(order.id);
Navigator.pop(context);

              },

do this instead of your code. It is saying that remove order is calling after the navigator.pop(context);. Which means the screen is popped out but the function is calling for the screen.