How to check which the current Route is?

前端 未结 11 604
栀梦
栀梦 2021-02-03 17:49

I want to navigate to different Routes using a Drawer, though I do not want to open a new instance of a Route each time I tap on it if I am already on that Route, rather I would

11条回答
  •  逝去的感伤
    2021-02-03 18:01

    You could just use one:

    Get.currentRoute
    

    with this package: https://pub.dev/packages/get

    And you would have the current route.

    However you wouldn't need this, because unlike the standard Flutter, Get prevents the user from accidentally clicking a button twice (if the user is on a low-cost device it will happen after a jank). Then you would only need to use: Get.to(Home()); and ready, it would be done!

    Currently, Getx is practically indispensable for projects with Flutter for this reason. I looked at all the questions, and they are cool, but they usually involve using a popUntil to gain access to the route and this can seem like a hack when another developer reads your code. With GetX you will not have this double click problem, because it only navigates to another page of the same if you expressly disable it using Get.to(Page(), preventDuplicates: false); Remembering that this is the exception, in your case you could simply use: Get.to(Page()); and navigate to the next page without any risk of duplicate routes, because preventing that sort of thing already comes standard on it.

    Getting the current route also seems to be something that involves a lot of boilerplate, with Get you get the name of the current route (even if you don't have named routes it will give you the name of it anyway), using a simple: Get.currenteRoute.

    Well, it's a little late to answer that, but I hope you and other users who read this enjoy

提交回复
热议问题