[go_router_builder] Fixed the return value of the generated push method (#3650)
Starting from version 6.5.0 [[reference](https://github.com/flutter/packages/blob/main/packages/go_router/CHANGELOG.md#650)] of the **go_router** package, the method **push** is able to return a Future value.
The generator was not updated to take that into account.
This PR fixes that.
The PR resolves the reported issue: https://github.com/flutter/flutter/issues/124487
diff --git a/packages/go_router_builder/CHANGELOG.md b/packages/go_router_builder/CHANGELOG.md
index da0e1f2..a7414a8 100644
--- a/packages/go_router_builder/CHANGELOG.md
+++ b/packages/go_router_builder/CHANGELOG.md
@@ -1,3 +1,7 @@
+## 1.2.2
+
+* Supports returning value in generated `push` method. [go_router CHANGELOG](https://github.com/flutter/packages/blob/main/packages/go_router/CHANGELOG.md#650)
+
## 1.2.1
* Supports opt-in required extra parameters. [#117261](https://github.com/flutter/flutter/issues/117261)
diff --git a/packages/go_router_builder/README.md b/packages/go_router_builder/README.md
index e911d33..6632625 100644
--- a/packages/go_router_builder/README.md
+++ b/packages/go_router_builder/README.md
@@ -165,6 +165,17 @@
This is the point of typed routing: the error is found statically.
+## Return value
+
+Starting from `go_router` 6.5.0, pushing a route and subsequently popping it, can produce
+a return value. The generated routes also follow this functionality.
+
+```dart
+void _tap() async {
+ final result = await PersonRoute(pid: 'p1').go(context);
+}
+```
+
## Query parameters
Optional parameters (named or positional) indicate query parameters:
diff --git a/packages/go_router_builder/example/lib/all_types.g.dart b/packages/go_router_builder/example/lib/all_types.g.dart
index 0dc37d5..7cf692f 100644
--- a/packages/go_router_builder/example/lib/all_types.g.dart
+++ b/packages/go_router_builder/example/lib/all_types.g.dart
@@ -81,7 +81,7 @@
void go(BuildContext context) => context.go(location);
- void push(BuildContext context) => context.push(location);
+ Future<T?> push<T>(BuildContext context) => context.push<T>(location);
void pushReplacement(BuildContext context) =>
context.pushReplacement(location);
@@ -103,7 +103,7 @@
void go(BuildContext context) => context.go(location);
- void push(BuildContext context) => context.push(location);
+ Future<T?> push<T>(BuildContext context) => context.push<T>(location);
void pushReplacement(BuildContext context) =>
context.pushReplacement(location);
@@ -133,7 +133,7 @@
void go(BuildContext context) => context.go(location);
- void push(BuildContext context) => context.push(location);
+ Future<T?> push<T>(BuildContext context) => context.push<T>(location);
void pushReplacement(BuildContext context) =>
context.pushReplacement(location);
@@ -157,7 +157,7 @@
void go(BuildContext context) => context.go(location);
- void push(BuildContext context) => context.push(location);
+ Future<T?> push<T>(BuildContext context) => context.push<T>(location);
void pushReplacement(BuildContext context) =>
context.pushReplacement(location);
@@ -187,7 +187,7 @@
void go(BuildContext context) => context.go(location);
- void push(BuildContext context) => context.push(location);
+ Future<T?> push<T>(BuildContext context) => context.push<T>(location);
void pushReplacement(BuildContext context) =>
context.pushReplacement(location);
@@ -213,7 +213,7 @@
void go(BuildContext context) => context.go(location);
- void push(BuildContext context) => context.push(location);
+ Future<T?> push<T>(BuildContext context) => context.push<T>(location);
void pushReplacement(BuildContext context) =>
context.pushReplacement(location);
@@ -239,7 +239,7 @@
void go(BuildContext context) => context.go(location);
- void push(BuildContext context) => context.push(location);
+ Future<T?> push<T>(BuildContext context) => context.push<T>(location);
void pushReplacement(BuildContext context) =>
context.pushReplacement(location);
@@ -271,7 +271,7 @@
void go(BuildContext context) => context.go(location);
- void push(BuildContext context) => context.push(location);
+ Future<T?> push<T>(BuildContext context) => context.push<T>(location);
void pushReplacement(BuildContext context) =>
context.pushReplacement(location);
@@ -303,7 +303,7 @@
void go(BuildContext context) => context.go(location);
- void push(BuildContext context) => context.push(location);
+ Future<T?> push<T>(BuildContext context) => context.push<T>(location);
void pushReplacement(BuildContext context) =>
context.pushReplacement(location);
@@ -329,7 +329,7 @@
void go(BuildContext context) => context.go(location);
- void push(BuildContext context) => context.push(location);
+ Future<T?> push<T>(BuildContext context) => context.push<T>(location);
void pushReplacement(BuildContext context) =>
context.pushReplacement(location);
@@ -350,7 +350,7 @@
void go(BuildContext context) => context.go(location);
- void push(BuildContext context) => context.push(location);
+ Future<T?> push<T>(BuildContext context) => context.push<T>(location);
void pushReplacement(BuildContext context) =>
context.pushReplacement(location);
@@ -466,7 +466,7 @@
void go(BuildContext context) => context.go(location);
- void push(BuildContext context) => context.push(location);
+ Future<T?> push<T>(BuildContext context) => context.push<T>(location);
void pushReplacement(BuildContext context) =>
context.pushReplacement(location);
@@ -580,7 +580,7 @@
void go(BuildContext context) => context.go(location);
- void push(BuildContext context) => context.push(location);
+ Future<T?> push<T>(BuildContext context) => context.push<T>(location);
void pushReplacement(BuildContext context) =>
context.pushReplacement(location);
diff --git a/packages/go_router_builder/example/lib/extra_example.g.dart b/packages/go_router_builder/example/lib/extra_example.g.dart
index b9febb4..1a090c3 100644
--- a/packages/go_router_builder/example/lib/extra_example.g.dart
+++ b/packages/go_router_builder/example/lib/extra_example.g.dart
@@ -31,7 +31,8 @@
void go(BuildContext context) => context.go(location, extra: $extra);
- void push(BuildContext context) => context.push(location, extra: $extra);
+ Future<T?> push<T>(BuildContext context) =>
+ context.push<T>(location, extra: $extra);
void pushReplacement(BuildContext context) =>
context.pushReplacement(location, extra: $extra);
@@ -54,7 +55,8 @@
void go(BuildContext context) => context.go(location, extra: $extra);
- void push(BuildContext context) => context.push(location, extra: $extra);
+ Future<T?> push<T>(BuildContext context) =>
+ context.push<T>(location, extra: $extra);
void pushReplacement(BuildContext context) =>
context.pushReplacement(location, extra: $extra);
@@ -74,7 +76,7 @@
void go(BuildContext context) => context.go(location);
- void push(BuildContext context) => context.push(location);
+ Future<T?> push<T>(BuildContext context) => context.push<T>(location);
void pushReplacement(BuildContext context) =>
context.pushReplacement(location);
diff --git a/packages/go_router_builder/example/lib/main.dart b/packages/go_router_builder/example/lib/main.dart
index f6a64ed..cf917c5 100644
--- a/packages/go_router_builder/example/lib/main.dart
+++ b/packages/go_router_builder/example/lib/main.dart
@@ -76,7 +76,8 @@
],
),
],
- )
+ ),
+ TypedGoRoute<FamilyCountRoute>(path: 'family-count/:count'),
],
)
class HomeRoute extends GoRouteData {
@@ -149,6 +150,17 @@
}
}
+class FamilyCountRoute extends GoRouteData {
+ const FamilyCountRoute(this.count);
+
+ final int count;
+
+ @override
+ Widget build(BuildContext context, GoRouterState state) => FamilyCountScreen(
+ count: count,
+ );
+}
+
class HomeScreen extends StatelessWidget {
const HomeScreen({super.key});
@@ -161,14 +173,38 @@
title: const Text(App.title),
centerTitle: true,
actions: <Widget>[
- ElevatedButton(
- onPressed: () => const PersonRoute('f1', 1).push(context),
- child: const Text('Push a route'),
- ),
- IconButton(
- onPressed: info.logout,
- tooltip: 'Logout: ${info.userName}',
- icon: const Icon(Icons.logout),
+ PopupMenuButton<String>(
+ itemBuilder: (BuildContext context) {
+ return <PopupMenuItem<String>>[
+ PopupMenuItem<String>(
+ value: '1',
+ child: const Text('Push w/o return value'),
+ onTap: () => const PersonRoute('f1', 1).push(context),
+ ),
+ PopupMenuItem<String>(
+ value: '2',
+ child: const Text('Push w/ return value'),
+ onTap: () async {
+ FamilyCountRoute(familyData.length)
+ .push<int>(context)
+ .then((int? value) {
+ if (value != null) {
+ ScaffoldMessenger.of(context).showSnackBar(
+ SnackBar(
+ content: Text('Age was: $value'),
+ ),
+ );
+ }
+ });
+ },
+ ),
+ PopupMenuItem<String>(
+ value: '3',
+ child: Text('Logout: ${info.userName}'),
+ onTap: () => info.logout(),
+ ),
+ ];
+ },
),
],
),
@@ -277,6 +313,35 @@
);
}
+class FamilyCountScreen extends StatelessWidget {
+ const FamilyCountScreen({super.key, required this.count});
+
+ final int count;
+
+ @override
+ Widget build(BuildContext context) => Scaffold(
+ appBar: AppBar(title: const Text('Family Count')),
+ body: Padding(
+ padding: const EdgeInsets.all(16.0),
+ child: Column(
+ crossAxisAlignment: CrossAxisAlignment.stretch,
+ children: <Widget>[
+ Center(
+ child: Text(
+ 'There are $count families',
+ style: Theme.of(context).textTheme.headlineSmall,
+ ),
+ ),
+ ElevatedButton(
+ onPressed: () => context.pop(count),
+ child: Text('Pop with return value $count'),
+ ),
+ ],
+ ),
+ ),
+ );
+}
+
class LoginScreen extends StatelessWidget {
const LoginScreen({this.from, super.key});
final String? from;
diff --git a/packages/go_router_builder/example/lib/main.g.dart b/packages/go_router_builder/example/lib/main.g.dart
index ba15ccd..864e8e1 100644
--- a/packages/go_router_builder/example/lib/main.g.dart
+++ b/packages/go_router_builder/example/lib/main.g.dart
@@ -33,6 +33,10 @@
),
],
),
+ GoRouteData.$route(
+ path: 'family-count/:count',
+ factory: $FamilyCountRouteExtension._fromState,
+ ),
],
);
@@ -45,7 +49,7 @@
void go(BuildContext context) => context.go(location);
- void push(BuildContext context) => context.push(location);
+ Future<T?> push<T>(BuildContext context) => context.push<T>(location);
void pushReplacement(BuildContext context) =>
context.pushReplacement(location);
@@ -62,7 +66,7 @@
void go(BuildContext context) => context.go(location);
- void push(BuildContext context) => context.push(location);
+ Future<T?> push<T>(BuildContext context) => context.push<T>(location);
void pushReplacement(BuildContext context) =>
context.pushReplacement(location);
@@ -80,7 +84,7 @@
void go(BuildContext context) => context.go(location);
- void push(BuildContext context) => context.push(location);
+ Future<T?> push<T>(BuildContext context) => context.push<T>(location);
void pushReplacement(BuildContext context) =>
context.pushReplacement(location);
@@ -101,12 +105,30 @@
void go(BuildContext context) => context.go(location, extra: $extra);
- void push(BuildContext context) => context.push(location, extra: $extra);
+ Future<T?> push<T>(BuildContext context) =>
+ context.push<T>(location, extra: $extra);
void pushReplacement(BuildContext context) =>
context.pushReplacement(location, extra: $extra);
}
+extension $FamilyCountRouteExtension on FamilyCountRoute {
+ static FamilyCountRoute _fromState(GoRouterState state) => FamilyCountRoute(
+ int.parse(state.params['count']!),
+ );
+
+ String get location => GoRouteData.$location(
+ '/family-count/${Uri.encodeComponent(count.toString())}',
+ );
+
+ void go(BuildContext context) => context.go(location);
+
+ Future<T?> push<T>(BuildContext context) => context.push<T>(location);
+
+ void pushReplacement(BuildContext context) =>
+ context.pushReplacement(location);
+}
+
const _$PersonDetailsEnumMap = {
PersonDetails.hobbies: 'hobbies',
PersonDetails.favoriteFood: 'favorite-food',
@@ -137,7 +159,7 @@
void go(BuildContext context) => context.go(location);
- void push(BuildContext context) => context.push(location);
+ Future<T?> push<T>(BuildContext context) => context.push<T>(location);
void pushReplacement(BuildContext context) =>
context.pushReplacement(location);
diff --git a/packages/go_router_builder/example/lib/shell_route_example.g.dart b/packages/go_router_builder/example/lib/shell_route_example.g.dart
index 5315025..e3544c7 100644
--- a/packages/go_router_builder/example/lib/shell_route_example.g.dart
+++ b/packages/go_router_builder/example/lib/shell_route_example.g.dart
@@ -27,7 +27,7 @@
void go(BuildContext context) => context.go(location);
- void push(BuildContext context) => context.push(location);
+ Future<T?> push<T>(BuildContext context) => context.push<T>(location);
void pushReplacement(BuildContext context) =>
context.pushReplacement(location);
@@ -61,7 +61,7 @@
void go(BuildContext context) => context.go(location);
- void push(BuildContext context) => context.push(location);
+ Future<T?> push<T>(BuildContext context) => context.push<T>(location);
void pushReplacement(BuildContext context) =>
context.pushReplacement(location);
@@ -76,7 +76,7 @@
void go(BuildContext context) => context.go(location);
- void push(BuildContext context) => context.push(location);
+ Future<T?> push<T>(BuildContext context) => context.push<T>(location);
void pushReplacement(BuildContext context) =>
context.pushReplacement(location);
diff --git a/packages/go_router_builder/example/lib/shell_route_with_keys_example.g.dart b/packages/go_router_builder/example/lib/shell_route_with_keys_example.g.dart
index 523af4e..a8a5101 100644
--- a/packages/go_router_builder/example/lib/shell_route_with_keys_example.g.dart
+++ b/packages/go_router_builder/example/lib/shell_route_with_keys_example.g.dart
@@ -48,7 +48,7 @@
void go(BuildContext context) => context.go(location);
- void push(BuildContext context) => context.push(location);
+ Future<T?> push<T>(BuildContext context) => context.push<T>(location);
void pushReplacement(BuildContext context) =>
context.pushReplacement(location);
@@ -64,7 +64,7 @@
void go(BuildContext context) => context.go(location);
- void push(BuildContext context) => context.push(location);
+ Future<T?> push<T>(BuildContext context) => context.push<T>(location);
void pushReplacement(BuildContext context) =>
context.pushReplacement(location);
@@ -81,7 +81,7 @@
void go(BuildContext context) => context.go(location);
- void push(BuildContext context) => context.push(location);
+ Future<T?> push<T>(BuildContext context) => context.push<T>(location);
void pushReplacement(BuildContext context) =>
context.pushReplacement(location);
diff --git a/packages/go_router_builder/example/lib/simple_example.g.dart b/packages/go_router_builder/example/lib/simple_example.g.dart
index 4edd92a..16538ef 100644
--- a/packages/go_router_builder/example/lib/simple_example.g.dart
+++ b/packages/go_router_builder/example/lib/simple_example.g.dart
@@ -32,7 +32,7 @@
void go(BuildContext context) => context.go(location);
- void push(BuildContext context) => context.push(location);
+ Future<T?> push<T>(BuildContext context) => context.push<T>(location);
void pushReplacement(BuildContext context) =>
context.pushReplacement(location);
@@ -49,7 +49,7 @@
void go(BuildContext context) => context.go(location);
- void push(BuildContext context) => context.push(location);
+ Future<T?> push<T>(BuildContext context) => context.push<T>(location);
void pushReplacement(BuildContext context) =>
context.pushReplacement(location);
diff --git a/packages/go_router_builder/example/test/widget_test.dart b/packages/go_router_builder/example/test/widget_test.dart
index aa6061a..e53f912 100644
--- a/packages/go_router_builder/example/test/widget_test.dart
+++ b/packages/go_router_builder/example/test/widget_test.dart
@@ -2,6 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
+import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:go_router_builder_example/main.dart';
@@ -12,13 +13,24 @@
await tester.tap(find.text('Login'));
await tester.pumpAndSettle();
- await tester.tap(find.text('Push a route'));
+ await _openPopupMenu(tester);
+
+ await tester.tap(find.text('Push w/o return value'));
await tester.pumpAndSettle();
expect(find.text('Chris'), findsOneWidget);
await tester.pageBack();
await tester.pumpAndSettle();
+ await _openPopupMenu(tester);
+
+ await tester.tap(find.text('Push w/ return value'));
+ await tester.pumpAndSettle();
+ expect(find.text('Family Count'), findsOneWidget);
+
+ await tester.pageBack();
+ await tester.pumpAndSettle();
+
await tester.tap(find.text('Sells'));
await tester.pumpAndSettle();
@@ -39,3 +51,11 @@
expect(find.text('Extra click count: 1'), findsOneWidget);
});
}
+
+Future<void> _openPopupMenu(WidgetTester tester) async {
+ final Finder moreButton = find.byIcon(Icons.more_vert);
+ expect(moreButton, findsOneWidget);
+
+ await tester.tap(moreButton);
+ await tester.pumpAndSettle();
+}
diff --git a/packages/go_router_builder/lib/src/route_config.dart b/packages/go_router_builder/lib/src/route_config.dart
index 8d09418..50d7e77 100644
--- a/packages/go_router_builder/lib/src/route_config.dart
+++ b/packages/go_router_builder/lib/src/route_config.dart
@@ -205,8 +205,8 @@
void go(BuildContext context) =>
context.go(location${_extraParam != null ? ', extra: $extraFieldName' : ''});
- void push(BuildContext context) =>
- context.push(location${_extraParam != null ? ', extra: $extraFieldName' : ''});
+ Future<T?> push<T>(BuildContext context) =>
+ context.push<T>(location${_extraParam != null ? ', extra: $extraFieldName' : ''});
void pushReplacement(BuildContext context) =>
context.pushReplacement(location${_extraParam != null ? ', extra: $extraFieldName' : ''});
diff --git a/packages/go_router_builder/pubspec.yaml b/packages/go_router_builder/pubspec.yaml
index fe1af19..11726b6 100644
--- a/packages/go_router_builder/pubspec.yaml
+++ b/packages/go_router_builder/pubspec.yaml
@@ -2,7 +2,7 @@
description: >-
A builder that supports generated strongly-typed route helpers for
package:go_router
-version: 1.2.1
+version: 1.2.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
diff --git a/packages/go_router_builder/test/test_inputs/_go_router_builder_test_input.dart b/packages/go_router_builder/test/test_inputs/_go_router_builder_test_input.dart
index 4a8a095..1e39c20 100644
--- a/packages/go_router_builder/test/test_inputs/_go_router_builder_test_input.dart
+++ b/packages/go_router_builder/test/test_inputs/_go_router_builder_test_input.dart
@@ -79,7 +79,7 @@
void go(BuildContext context) => context.go(location);
- void push(BuildContext context) => context.push(location);
+ Future<T?> push<T>(BuildContext context) => context.push<T>(location);
void pushReplacement(BuildContext context) =>
context.pushReplacement(location);
@@ -131,7 +131,7 @@
void go(BuildContext context) => context.go(location);
- void push(BuildContext context) => context.push(location);
+ Future<T?> push<T>(BuildContext context) => context.push<T>(location);
void pushReplacement(BuildContext context) =>
context.pushReplacement(location);
@@ -173,7 +173,8 @@
void go(BuildContext context) => context.go(location, extra: $extra);
- void push(BuildContext context) => context.push(location, extra: $extra);
+ Future<T?> push<T>(BuildContext context) =>
+ context.push<T>(location, extra: $extra);
void pushReplacement(BuildContext context) =>
context.pushReplacement(location, extra: $extra);
@@ -213,7 +214,8 @@
void go(BuildContext context) => context.go(location, extra: $extra);
- void push(BuildContext context) => context.push(location, extra: $extra);
+ Future<T?> push<T>(BuildContext context) =>
+ context.push<T>(location, extra: $extra);
void pushReplacement(BuildContext context) =>
context.pushReplacement(location, extra: $extra);
@@ -259,7 +261,7 @@
void go(BuildContext context) => context.go(location);
- void push(BuildContext context) => context.push(location);
+ Future<T?> push<T>(BuildContext context) => context.push<T>(location);
void pushReplacement(BuildContext context) =>
context.pushReplacement(location);
@@ -312,7 +314,7 @@
void go(BuildContext context) => context.go(location);
- void push(BuildContext context) => context.push(location);
+ Future<T?> push<T>(BuildContext context) => context.push<T>(location);
void pushReplacement(BuildContext context) =>
context.pushReplacement(location);