Skip to content

Return value when pop#3368

Merged
auto-submit[bot] merged 23 commits into
flutter:mainfrom
NazarenoCavazzon:feat/return-value-when-pop
Mar 23, 2023
Merged

Return value when pop#3368
auto-submit[bot] merged 23 commits into
flutter:mainfrom
NazarenoCavazzon:feat/return-value-when-pop

Conversation

@NazarenoCavazzon

@NazarenoCavazzon NazarenoCavazzon commented Mar 3, 2023

Copy link
Copy Markdown
Contributor

In this PR I'm modifying the push and pushNamed by making them return a Future<T?>, and using completers to make the users able to return an wait values from within pages.

Fixes flutter/flutter#99663.
Fixes flutter/flutter#107217
Fixes flutter/flutter#100969

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

Pre-launch Checklist

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

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

@NazarenoCavazzon

Copy link
Copy Markdown
Contributor Author

@chunhtai Here it is

Comment thread packages/go_router/example/pubspec.yaml Outdated
go_router:
path: ..
logging: ^1.0.0
package_info_plus_web: ^2.0.0

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can this be removed?

Comment thread packages/go_router/CHANGELOG.md Outdated

## 6.3.0

- Support for returning values on pop.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- Support for returning values on pop.
- Supports for returning values on pop.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please move the item under ## NEXT to be under 6.3.0 as well

Comment thread packages/go_router/doc/navigation.md Outdated
```dart
onTap: () {
final bool? result = await context.push<bool>('/page2');
WidgetsBinding.instance.addPostFrameCallback((_) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why a post frame callback?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was before context.mounted was implemented, and using context after the value was returned lead to some dirty context problems, but we can take it out

final _NavigatorStateIterator iterator = _createNavigatorStateIterator();
while (iterator.moveNext()) {
if (iterator.current.canPop()) {
iterator.matchList.last.completer?.complete(result);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of exposing the completer directly, can you add a method in RouteMatch something like didComplete()? and let ImperativeRouteMatch to override the method to call complete on the completer

Comment thread packages/go_router/lib/src/match.dart Outdated
required String parentSubloc, // e.g. /family/f2
required Map<String, String> pathParameters,
required Object? extra,
Completer<dynamic>? completer,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you use method this property should be in ImerativeRouteMatch, and instead of dynamic, it should use generic type T

error: matches.last.error,
pageKey: pageKey,
matches: matches,
completer: completer,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not let ImperativeRouteMatch to create the completer itself.

required super.error,
required super.pageKey,
required this.matches,
super.completer,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Somewhere in the doc should mention about the completer and future and what it meant.

also, please remove the todo for me. Thanks in advance

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@chunhtai

chunhtai commented Mar 3, 2023

Copy link
Copy Markdown
Contributor

Since this is not really a breaking change, so you don't need to write a breaking change doc. Consider moving the snippet in the doc into the example folder

@NazarenoCavazzon

Copy link
Copy Markdown
Contributor Author

let me know if there's another change to make

Comment thread packages/go_router/CHANGELOG.md Outdated

- Supports for returning values on pop.

## NEXT

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant the ##Next section should be merged in 6.3.0

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

## Next meant whoever publish the next version will need to publish the changes in##NEXTas well

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AHHHHH, got it

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems not address yet?

Comment thread packages/go_router/lib/src/match.dart Outdated
}

/// Completes the promise returned by [GoRouter.push].
void complete([T? value]) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the entire completer should be move to ImperativeRouteMatch. This can just be a empty implementation for subclass override

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also maybe change this to didComplete. it is a special keyword in the framework, that has specific meaning

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Comment thread packages/go_router/lib/src/match.dart Outdated
throw MatcherError('Unexpected route type: $route', restLoc);
}

/// Completes the promise returned by [GoRouter.push].

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should document what the purpose of this method

Comment thread packages/go_router/lib/src/match.dart Outdated
/// ```dart
/// context.pop(true);
/// ```
void complete([dynamic value]) {}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@chunhtai what do you think?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why optional? I think it can be Object? value.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant this method should be call didComplete.

void didComplete(Object? value) {}

Comment thread packages/go_router/lib/src/match.dart Outdated
throw MatcherError('Unexpected route type: $route', restLoc);
}

/// Completes the promise returned by [GoRouter.push], allowing comunication

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe:

Called when the corresponding [Route] associated with this route match is completed.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Comment thread packages/go_router/lib/src/match.dart Outdated
/// Completes the promise returned by [GoRouter.push], allowing comunication
/// between pages.
///
/// If the promise has already been completed, this method does nothing.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

promise is not a keyword in dart. I think you meant future. Eitherway, this is implementation detail, we should focus on when/why this method is called.

Comment thread packages/go_router/lib/src/match.dart Outdated
/// ```dart
/// context.pop(true);
/// ```
void complete([dynamic value]) {}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why optional? I think it can be Object? value.


/// The future of the [RouteMatch] completer. When the future completes, this
/// will return the value passed to [complete].
Future<T?> get future => _completer.future;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this can be private

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem is we need to return this completer future in the push function, if we make it private we cannot return it.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can access private method as long as the class is in the same file, so it should be fine

Comment thread packages/go_router/lib/src/match.dart Outdated
/// ```dart
/// context.pop(true);
/// ```
void complete([dynamic value]) {}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant this method should be call didComplete.

void didComplete(Object? value) {}

Comment thread packages/go_router/lib/src/parser.dart Outdated
if (configuration.matches.last is ImperativeRouteMatch) {
configuration =
(configuration.matches.last as ImperativeRouteMatch).matches;
(configuration.matches.last as ImperativeRouteMatch<dynamic>).matches;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can this be Object? ?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should avoid using dynamic as much as possible unless you are handling json.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@chunhtai chunhtai left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, just some nits and change log


/// The future of the [RouteMatch] completer. When the future completes, this
/// will return the value passed to [complete].
Future<T?> get future => _completer.future;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can access private method as long as the class is in the same file, so it should be fine

_completer.complete(value as T?);
}

/// The future of the [RouteMatch] completer. When the future completes, this

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// The future of the [RouteMatch] completer. When the future completes, this
/// The future of the [RouteMatch] completer.
///
/// When the future completes, this

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didn't know about private methods being accessed if in the same file, really cool!

Comment thread packages/go_router/CHANGELOG.md Outdated

- Supports for returning values on pop.

## NEXT

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems not address yet?

@chunhtai

chunhtai commented Mar 3, 2023

Copy link
Copy Markdown
Contributor

and the analyzer is not happy

@chunhtai chunhtai requested a review from hannah-hyj March 3, 2023 23:16
@NazarenoCavazzon

Copy link
Copy Markdown
Contributor Author

@chunhtai fixed the change log, almost forgot.

Comment thread packages/go_router/CHANGELOG.md Outdated
NazarenoCavazzon and others added 2 commits March 3, 2023 22:08
@booooohdan

Copy link
Copy Markdown

Waiting for resolving this PR

@hannah-hyj hannah-hyj left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@chunhtai chunhtai added the autosubmit Merge PR when tree becomes green via auto submit App label Mar 20, 2023
@chunhtai

Copy link
Copy Markdown
Contributor

It looks like there is some conflict, can you rebase off the latest main?

@auto-submit auto-submit Bot removed the autosubmit Merge PR when tree becomes green via auto submit App label Mar 23, 2023
@auto-submit

auto-submit Bot commented Mar 23, 2023

Copy link
Copy Markdown
Contributor

auto label is removed for flutter/packages, pr: 3368, Failed to merge pr#: 3368 with Pull request could not be merged: Pull Request is not mergeable.

@NazarenoCavazzon

Copy link
Copy Markdown
Contributor Author

It looks like there is some conflict, can you rebase off the latest main?

Done

@@ -1,5 +1,6 @@
## 6.3.0

- Supports returning values on pop.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please publish a new version

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated to 6.5.0

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like the conflict is back :(

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, I'm resolving them rn

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Everything is a mess hahahaha, I'll rebase it to main and made the implementation again 🙃

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@chunhtai now there shouldn't be any more conflicts

@chunhtai chunhtai left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@chunhtai chunhtai added the autosubmit Merge PR when tree becomes green via auto submit App label Mar 23, 2023
@auto-submit auto-submit Bot merged commit 1f67d0c into flutter:main Mar 23, 2023
@tomassasovsky

Copy link
Copy Markdown
Contributor

grande naza!

@NazarenoCavazzon

Copy link
Copy Markdown
Contributor Author

Niceeeeeeeeee

engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Mar 24, 2023
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Mar 24, 2023
@blendthink blendthink mentioned this pull request Mar 28, 2023
5 tasks
nploi pushed a commit to nploi/packages that referenced this pull request Jul 16, 2023
creatorpiyush pushed a commit to creatorpiyush/packages that referenced this pull request Jun 10, 2026
bisor0627 pushed a commit to bisor0627/packages that referenced this pull request Jun 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

autosubmit Merge PR when tree becomes green via auto submit App p: go_router

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[go_router] callback function for go_router. [go_router] Update previous page on pop [go_router] Support return data from a screen with pop()

6 participants