1

In my widget witch is a StatefulWidget, my logic check a token and in error case, I navigate to my home page.

I'm waiting the widget finish to build by using this method:

WidgetsBinding.instance.addPostFrameCallback((_) {
    Navigator.pushNamed(context, '/');
});

But the problem is that even if I'm not focus on this widget anymore, he will still run in background and make a redirection in loop.

In my logic, I use setState once if my accesToken is expired. After that the widget rebuild and I check with the new token send my server, and if he is empty I redirect my user to the home page. I think the problem come from the setState.

So, is there a way to kill or dispose this widget after leaving ? Or maybe an other way to navigate ?

Lab
  • 1,237
  • 2
  • 13
  • 30

1 Answers1

0

So I'm gonna answer my own question, after several tests I found the solution.

I can break the loop by using Navigator.popAndPushNamed(context, '/')); instead of pushNamed, it will pop the route off the navigator and so the widget will stop running on background when the parent is rebuild.

Lab
  • 1,237
  • 2
  • 13
  • 30