Get the previous route name in Flutter

1,393

Solution 1

You can use getx pack to get previous Route

Get.previousRoute;

get current Route

Get.currentRoute;

Solution 2

Well, upon further investigation, there is no way to know the previous page.

I fixed this sending an argument previousPage with the route name value.

You can also use a NavigatorObserver.

Share:
1,393
Nicolas Lucero
Author by

Nicolas Lucero

Updated on December 24, 2022

Comments

  • Nicolas Lucero
    Nicolas Lucero over 1 year

    I understand that Flutter routes works like a stack, so I want to know the previous route name which I came from.

    I'm using a preview page to show a picture, but this picture can come from the camera or the gallery, I'd like to show a dialog only if it came from gallery.

    I read I have to use an observer, but I think there is an easy way to do that.

    Thanks in advance.

  • Nicolas Lucero
    Nicolas Lucero over 2 years
    Thank you, I think this is the best way to do it and the most accurate answer.
  • omega_mi
    omega_mi about 2 years
    Can you give example how NavigatorObserver can retrieve previous route name ?
  • Nicolas Lucero
    Nicolas Lucero almost 2 years
    MaterialApp has an attr called navigatorObservers. You can see an example here: stackoverflow.com/questions/46165705/…