Skip to content

Commit 4595c41

Browse files
authored
[go_router] guard context access in then clauses (#6685)
Prepares for a fix to the `use_build_context_synchronously` lint (https://dart-review.googlesource.com/c/sdk/+/365541) that will complain about these unsafe usages.
1 parent 55499d8 commit 4595c41

File tree

4 files changed

+12
-1
lines changed

4 files changed

+12
-1
lines changed

packages/go_router/lib/src/parser.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ class GoRouteInformationParser extends RouteInformationParser<RouteMatchList> {
7272
return debugParserFuture = _redirect(context, matchList)
7373
.then<RouteMatchList>((RouteMatchList value) {
7474
if (value.isError && onParserException != null) {
75+
// TODO(chunhtai): Figure out what to return if context is invalid.
76+
// ignore: use_build_context_synchronously
7577
return onParserException!(context, value);
7678
}
7779
return value;
@@ -106,6 +108,8 @@ class GoRouteInformationParser extends RouteInformationParser<RouteMatchList> {
106108
initialMatches,
107109
).then<RouteMatchList>((RouteMatchList matchList) {
108110
if (matchList.isError && onParserException != null) {
111+
// TODO(chunhtai): Figure out what to return if context is invalid.
112+
// ignore: use_build_context_synchronously
109113
return onParserException!(context, matchList);
110114
}
111115

packages/go_router_builder/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 2.6.2
2+
3+
* Fixes a bug in the example app when accessing `BuildContext`.
4+
15
## 2.6.1
26

37
* Fixes typo in `durationDecoderHelperName`.

packages/go_router_builder/example/lib/main.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,9 @@ class HomeScreen extends StatelessWidget {
188188
unawaited(FamilyCountRoute(familyData.length)
189189
.push<int>(context)
190190
.then((int? value) {
191+
if (!context.mounted) {
192+
return;
193+
}
191194
if (value != null) {
192195
ScaffoldMessenger.of(context).showSnackBar(
193196
SnackBar(

packages/go_router_builder/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: go_router_builder
22
description: >-
33
A builder that supports generated strongly-typed route helpers for
44
package:go_router
5-
version: 2.6.1
5+
version: 2.6.2
66
repository: https://github.com/flutter/packages/tree/main/packages/go_router_builder
77
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+go_router_builder%22
88

0 commit comments

Comments
 (0)