-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Return value when pop #3368
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Return value when pop #3368
Changes from 7 commits
f74ad0d
72db2d6
ba08b81
845dfbd
0105de8
4a6352c
ecee639
98e1216
1b5e37b
b734714
ba99cc5
9b556a3
8d34c15
0f0eafc
77f6ab9
251966a
ed87847
1661569
c3ed204
02a4f5c
a44a5cc
801c7c6
5d88336
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,7 @@ | ||
| ## 6.3.0 | ||
|
|
||
| - Supports for returning values on pop. | ||
|
|
||
| ## NEXT | ||
|
||
|
|
||
| - Updates compileSdkVersion to 33. | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -76,15 +76,15 @@ class GoRouterDelegate extends RouterDelegate<RouteMatchList> | |||||||||
| } | ||||||||||
|
|
||||||||||
| /// Pushes the given location onto the page stack | ||||||||||
| void push(RouteMatchList matches) { | ||||||||||
| Future<T?> push<T extends Object?>(RouteMatchList matches) async { | ||||||||||
| assert(matches.last.route is! ShellRoute); | ||||||||||
|
|
||||||||||
| // Remap the pageKey to allow any number of the same page on the stack | ||||||||||
| final int count = (_pushCounts[matches.fullpath] ?? 0) + 1; | ||||||||||
| _pushCounts[matches.fullpath] = count; | ||||||||||
| final ValueKey<String> pageKey = | ||||||||||
| ValueKey<String>('${matches.fullpath}-p$count'); | ||||||||||
| final ImperativeRouteMatch newPageKeyMatch = ImperativeRouteMatch( | ||||||||||
| final ImperativeRouteMatch<T> newPageKeyMatch = ImperativeRouteMatch<T>( | ||||||||||
| route: matches.last.route, | ||||||||||
| subloc: matches.last.subloc, | ||||||||||
| extra: matches.last.extra, | ||||||||||
|
|
@@ -95,6 +95,7 @@ class GoRouterDelegate extends RouterDelegate<RouteMatchList> | |||||||||
|
|
||||||||||
| _matchList.push(newPageKeyMatch); | ||||||||||
| notifyListeners(); | ||||||||||
| return newPageKeyMatch.future; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| /// Returns `true` if the active Navigator can pop. | ||||||||||
|
|
@@ -113,6 +114,7 @@ class GoRouterDelegate extends RouterDelegate<RouteMatchList> | |||||||||
| final _NavigatorStateIterator iterator = _createNavigatorStateIterator(); | ||||||||||
| while (iterator.moveNext()) { | ||||||||||
| if (iterator.current.canPop()) { | ||||||||||
| iterator.matchList.last.complete(result); | ||||||||||
| iterator.current.pop<T>(result); | ||||||||||
| return; | ||||||||||
| } | ||||||||||
|
|
@@ -268,8 +270,7 @@ class _NavigatorStateIterator extends Iterator<NavigatorState> { | |||||||||
| } | ||||||||||
|
|
||||||||||
| /// The route match that represent route pushed through [GoRouter.push]. | ||||||||||
| // TODO(chunhtai): Removes this once imperative API no longer insert route match. | ||||||||||
| class ImperativeRouteMatch extends RouteMatch { | ||||||||||
| class ImperativeRouteMatch<T> extends RouteMatch { | ||||||||||
| /// Constructor for [ImperativeRouteMatch]. | ||||||||||
| ImperativeRouteMatch({ | ||||||||||
| required super.route, | ||||||||||
|
|
@@ -278,8 +279,25 @@ class ImperativeRouteMatch extends RouteMatch { | |||||||||
| required super.error, | ||||||||||
| required super.pageKey, | ||||||||||
| required this.matches, | ||||||||||
| }); | ||||||||||
| }) : _completer = Completer<T?>(); | ||||||||||
|
|
||||||||||
| /// The matches that produces this route match. | ||||||||||
| final RouteMatchList matches; | ||||||||||
|
|
||||||||||
| /// The completer for the promise returned by [GoRouter.push]. | ||||||||||
| final Completer<T?> _completer; | ||||||||||
|
|
||||||||||
| /// Completes the promise returned by [GoRouter.push]. | ||||||||||
| @override | ||||||||||
| void complete([dynamic value]) { | ||||||||||
| _completer.complete(value as T?); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| /// Returns `true` if the promise returned by [GoRouter.push] has been completed. | ||||||||||
| @override | ||||||||||
| bool didComplete() => _completer.isCompleted; | ||||||||||
|
|
||||||||||
| /// The future of the [RouteMatch] completer. When the future completes, this | ||||||||||
|
||||||||||
| /// The future of the [RouteMatch] completer. When the future completes, this | |
| /// The future of the [RouteMatch] completer. | |
| /// | |
| /// When the future completes, this |
There was a problem hiding this comment.
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!
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this can be private
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -61,6 +61,29 @@ class RouteMatch { | |
| throw MatcherError('Unexpected route type: $route', restLoc); | ||
| } | ||
|
|
||
| /// Completes the promise returned by [GoRouter.push], allowing comunication | ||
|
||
| /// between pages. | ||
| /// | ||
| /// If the promise has already been completed, this method does nothing. | ||
|
||
| /// | ||
| /// E.g.: | ||
| /// ```dart | ||
| /// final bool? result = await context.push<bool>('/page2'); | ||
| /// if(result ?? false){ | ||
| /// // do something | ||
| /// } | ||
| /// ``` | ||
| /// When the page is popped, the promise is completed with the value passed, | ||
| /// and the push method returns. | ||
| /// to [Navigator.pop]. | ||
| /// ```dart | ||
| /// context.pop(true); | ||
| /// ``` | ||
| void complete([dynamic value]) {} | ||
|
||
|
|
||
| /// Returns `true` if the promise returned by [GoRouter.push] has been completed. | ||
| bool? didComplete() => null; | ||
|
|
||
| /// The matched route. | ||
| final RouteBase route; | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -104,7 +104,7 @@ class GoRouteInformationParser extends RouteInformationParser<RouteMatchList> { | |
| } | ||
| if (configuration.matches.last is ImperativeRouteMatch) { | ||
| configuration = | ||
| (configuration.matches.last as ImperativeRouteMatch).matches; | ||
| (configuration.matches.last as ImperativeRouteMatch<dynamic>).matches; | ||
|
||
| } | ||
| return RouteInformation( | ||
| location: configuration.uri.toString(), | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.