packages icon indicating copy to clipboard operation
packages copied to clipboard

[go_router] Return a value on pop

Open NazarenoCavazzon opened this issue 3 years ago • 13 comments

In this PR I'm adding 2 new methods, pushAsync and pushNamedAsync, that allows you to wait for a value when the widget pushed pops, like in navigator 1.0.

Fixes https://github.com/flutter/flutter/issues/99663. Fixes https://github.com/flutter/flutter/issues/107217 Fixes https://github.com/flutter/flutter/issues/100969

This PR will not conflict with older versions of go_router when updated.

Here's the link of the document with the changes: https://docs.google.com/document/d/1bcPV8yNAETmlxTFTMpO_JcJ7-yMoF8qaCPa884hFmXA/edit?usp=sharing&resourcekey=0-73tg8sZOVf0YXBmz-MLwQw.

Pre-launch Checklist

  • [x] I read the Contributor Guide and followed the process outlined there for submitting PRs.
  • [x] I read the Tree Hygiene wiki page, which explains my responsibilities.
  • [x] I read and followed the relevant style guides and ran the auto-formatter. (Unlike the flutter/flutter repo, the flutter/packages repo does use dart format.)
  • [x] I signed the CLA.
  • [x] The title of the PR starts with the name of the package surrounded by square brackets, e.g. [shared_preferences]
  • [x] I listed at least one issue that this PR fixes in the description above.
  • [x] I updated pubspec.yaml with an appropriate new version according to the pub versioning philosophy, or this PR is exempt from version changes.
  • [x] I updated CHANGELOG.md to add a description of the change, following repository CHANGELOG style.
  • [x] I updated/added relevant documentation (doc comments with ///).
  • [x] I added new tests to check the change I am making, or this PR is test-exempt.
  • [x] All existing and new tests are passing.

If you need help, consider asking for advice on the #hackers-new channel on Discord.

NazarenoCavazzon avatar Aug 04 '22 22:08 NazarenoCavazzon

This is a must-have! Please merge!

Rodsevich avatar Aug 04 '22 22:08 Rodsevich

Awesome! This is what’s stopping us from using go_router, hopefully is merged!

jorger5 avatar Aug 04 '22 22:08 jorger5

This would be great to have!

NicoCieri10 avatar Aug 04 '22 22:08 NicoCieri10

We must have it soon!

Matias-Olivier avatar Aug 04 '22 22:08 Matias-Olivier

I updated the PR, so now instead of doing:

final result = await context.pushNamedAsync(routeName) as bool?;
context.pop(someValue);

you can do:

final result = await context.pushNamedAsync<bool>(routeName);
context.pop<bool>(someValue);

Or even:

bool? result = await context.pushNamedAsync(routeName);

In both cases the returned value will be nullable.

NazarenoCavazzon avatar Aug 04 '22 23:08 NazarenoCavazzon

I think this feature might be a great addition. LGTM!

Mizxam avatar Aug 04 '22 23:08 Mizxam

Wow! nice job dude, i hope that this merge!

Loukanikos99 avatar Aug 05 '22 01:08 Loukanikos99

@giallon I made some changes

NazarenoCavazzon avatar Aug 05 '22 01:08 NazarenoCavazzon

Another great adition to flutter, nice job

NahuVidales avatar Aug 05 '22 03:08 NahuVidales

It would be great to have this added! it is so useful

ignacioss avatar Aug 05 '22 14:08 ignacioss

After some tests I came to the conclusion that you can use pushAsync as normal push, so because of that I decided to change those methods so we don't need to add new ones. Instead of:

final result = await context.pushNamedAsync(routeName) as bool?;
context.pop(someValue);

you do:

final result = await context.pushNamed<bool>(routeName);
context.pop<bool>(someValue);

and if you don't need any promise at all you can keep doing:

context.pushNamed(routeName);
context.pop();

NazarenoCavazzon avatar Aug 05 '22 15:08 NazarenoCavazzon

This feature is very good and it would be useful to have it.

Gomez-Enzo avatar Aug 05 '22 21:08 Gomez-Enzo

I updated the go_router_builder to start using element2

NazarenoCavazzon avatar Aug 08 '22 16:08 NazarenoCavazzon

Now because we save the completer in the RouteMatch it'll not break when pushing the same screen over and over again

NazarenoCavazzon avatar Aug 16 '22 18:08 NazarenoCavazzon

We should not merge this until we figure out this how to support imperative API moving forward https://github.com/flutter/flutter/issues/99112. Closing for now

chunhtai avatar Aug 18 '22 22:08 chunhtai

wait this issue...

tatsuyuki25 avatar Sep 02 '22 03:09 tatsuyuki25

I know this is a missing feature, and probably a big one. But we would like to do a refactoring on the imperative API which may make this fix obsolete or hard to maintain. Sorry for the inconvenience.

chunhtai avatar Sep 02 '22 16:09 chunhtai

One should be patient, for the sake of a good API, where flaws are temporary and respectable and rigorous.

ismanong avatar Sep 10 '22 13:09 ismanong

@chunhtai when do you think we can add this feature to go_router? I have a branch with the implementation updated and all the tests ready to be reviewed. Also that code has been in production in multiple apps working flawlessly.

NazarenoCavazzon avatar Nov 17 '22 01:11 NazarenoCavazzon

@chunhtai您认为我们什么时候可以将此功能添加到go_router?我有一个分支,其实现已更新,所有测试都已准备好接受审查。此外,该代码已经在多个应用程序中完美地工作。

I currently need to return to the previous page to carry parameters. How should I do it before you merge it? I don’t know how to do it

paintingStyle avatar Nov 27 '22 12:11 paintingStyle

I have a package called go_router_flow in pub, it's updated to the last go_router update, returning values work the same as navigator 1.0

NazarenoCavazzon avatar Nov 27 '22 16:11 NazarenoCavazzon

We should not merge this until we figure out this how to support imperative API moving forward flutter/flutter#99112. Closing for now

So we can't get async push and go until removeRoute, popUntil pushAndRemoveUntil, and replace are implemented? Why?

ChopinDavid avatar Jan 03 '23 22:01 ChopinDavid

@chunhtai any news?

NazarenoCavazzon avatar Jan 05 '23 14:01 NazarenoCavazzon

I think this is already fixed when i refactor the pop. See https://pub.dev/documentation/go_router/latest/go_router/GoRouter/pop.html

chunhtai avatar Jan 05 '23 17:01 chunhtai

I think this is already fixed when i refactor the pop. See https://pub.dev/documentation/go_router/latest/go_router/GoRouter/pop.html

Wich version this will be merged? I can't find this pop in API (6.0.1)

Katekko avatar Jan 18 '23 16:01 Katekko

Right, so now pop can come with a value after @chunhtai refactor. But how can I retrieve this value? Since both push and go return void and void can't be used? Someone understand how this will work?

maironLucasSlz avatar Mar 03 '23 17:03 maironLucasSlz

hmmm, I mainly added for show dialog and imperative API. We should probably expose return future for gorouter push.

chunhtai avatar Mar 03 '23 17:03 chunhtai

@chunhtai just to let you know, this PR is outdated. If you want I can make the update. I have another branch with a better/cleaner implementation of this.

NazarenoCavazzon avatar Mar 03 '23 17:03 NazarenoCavazzon

Sure feel free to open a new pr and pin me to review. I think what's left is the push need to return a future

chunhtai avatar Mar 03 '23 17:03 chunhtai