From c706bc6f54227af6657f8a8b305dda8bb5e9d4fa Mon Sep 17 00:00:00 2001 From: Michael Goderbauer Date: Tue, 7 May 2024 12:53:00 -0700 Subject: [PATCH] [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. --- packages/go_router/lib/src/parser.dart | 4 ++++ packages/go_router_builder/CHANGELOG.md | 4 ++++ packages/go_router_builder/example/lib/main.dart | 3 +++ packages/go_router_builder/pubspec.yaml | 2 +- 4 files changed, 12 insertions(+), 1 deletion(-) diff --git a/packages/go_router/lib/src/parser.dart b/packages/go_router/lib/src/parser.dart index bffcd1b15b62..b4115a1fca19 100644 --- a/packages/go_router/lib/src/parser.dart +++ b/packages/go_router/lib/src/parser.dart @@ -72,6 +72,8 @@ class GoRouteInformationParser extends RouteInformationParser { return debugParserFuture = _redirect(context, matchList) .then((RouteMatchList value) { if (value.isError && onParserException != null) { + // TODO(chunhtai): Figure out what to return if context is invalid. + // ignore: use_build_context_synchronously return onParserException!(context, value); } return value; @@ -106,6 +108,8 @@ class GoRouteInformationParser extends RouteInformationParser { initialMatches, ).then((RouteMatchList matchList) { if (matchList.isError && onParserException != null) { + // TODO(chunhtai): Figure out what to return if context is invalid. + // ignore: use_build_context_synchronously return onParserException!(context, matchList); } diff --git a/packages/go_router_builder/CHANGELOG.md b/packages/go_router_builder/CHANGELOG.md index 2a490e34200e..dd2b10f30a1e 100644 --- a/packages/go_router_builder/CHANGELOG.md +++ b/packages/go_router_builder/CHANGELOG.md @@ -1,3 +1,7 @@ +## 2.6.2 + +* Fixes a bug in the example app when accessing `BuildContext`. + ## 2.6.1 * Fixes typo in `durationDecoderHelperName`. diff --git a/packages/go_router_builder/example/lib/main.dart b/packages/go_router_builder/example/lib/main.dart index b67ca5f5aa85..d555649825ac 100644 --- a/packages/go_router_builder/example/lib/main.dart +++ b/packages/go_router_builder/example/lib/main.dart @@ -188,6 +188,9 @@ class HomeScreen extends StatelessWidget { unawaited(FamilyCountRoute(familyData.length) .push(context) .then((int? value) { + if (!context.mounted) { + return; + } if (value != null) { ScaffoldMessenger.of(context).showSnackBar( SnackBar( diff --git a/packages/go_router_builder/pubspec.yaml b/packages/go_router_builder/pubspec.yaml index c64c3341243e..82b41bb5f67a 100644 --- a/packages/go_router_builder/pubspec.yaml +++ b/packages/go_router_builder/pubspec.yaml @@ -2,7 +2,7 @@ name: go_router_builder description: >- A builder that supports generated strongly-typed route helpers for package:go_router -version: 2.6.1 +version: 2.6.2 repository: https://github.com/flutter/packages/tree/main/packages/go_router_builder issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+go_router_builder%22