Skip to content

[go_router] guard context access in then clauses #6685

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

Merged
merged 3 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/go_router/lib/src/parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class GoRouteInformationParser extends RouteInformationParser<RouteMatchList> {
return debugParserFuture = _redirect(context, matchList)
.then<RouteMatchList>((RouteMatchList value) {
if (value.isError && onParserException != null) {
return onParserException!(context, value);
return onParserException!(context, value); // ignore: use_build_context_synchronously
Copy link
Member Author

Choose a reason for hiding this comment

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

@chunhtai Here and below I am actually not sure what should happen if the context has become invalid. Do you have a suggestion?

Copy link
Contributor

Choose a reason for hiding this comment

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

if the context is invalid, it should just return a never complete future.

Something like return Completer<RouteMatchList>().future

}
return value;
});
Expand Down Expand Up @@ -106,7 +106,7 @@ class GoRouteInformationParser extends RouteInformationParser<RouteMatchList> {
initialMatches,
).then<RouteMatchList>((RouteMatchList matchList) {
if (matchList.isError && onParserException != null) {
return onParserException!(context, matchList);
return onParserException!(context, matchList); // ignore: use_build_context_synchronously
}

assert(() {
Expand Down
3 changes: 3 additions & 0 deletions packages/go_router_builder/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,9 @@ class HomeScreen extends StatelessWidget {
unawaited(FamilyCountRoute(familyData.length)
.push<int>(context)
.then((int? value) {
if (!context.mounted) {
return;
}
if (value != null) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
Expand Down