简体   繁体   中英

What is the best way to push replace with arguments in flutter?

I'm trying to find out the best routing solution for this scenario here. I have screens with workflow as follows.

Items Screen(1)   ===>  Item Details Screen(2)  ====>   Edit Items Screen(3)

On the Edit Item screen, when I edit things and save, it should take the user back to the Item details screen with some objects and variables passed as routing arguments.

I used pushReplacementNamed("/details", arguments: ItemDetailsArguments(item: item, variables: variables)); on a button click from the edit item screen.

This works fine, until I noticed when I click on the back button on the App bar (Item Details Screen(2)) it takes me all the way back to the Edit Item Screen(3) again instead of the Items Screen(1).

Is there a better way to pass data from screen 3 to 2 upon button click and then take the user all the way back to screen 1 when the back button is pressed from screen 2?

Instead of a pushReplacementNamed you can use pushNamedAndRemoveUntil with a predicate to remove the unwanted routes from the stack.

Read more about pushNamedAndRemoveUntil

pushNamedAndRemoveUntil(
   "/details", 
   ModalRoute.withName("/items"), // screen one - route predicate
   arguments: ItemDetailsArguments(item: item, variables: variables),
);

Route predicate will let you decide what needs to be removed from the stack.

In this case, after the button is clicked, push to the "details screen" and also remove all other routes that are in the stack til the main screen. Hope it helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM