Skip to content

Commit cd15620

Browse files
authored
[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: flutter/flutter#124487
1 parent 528aa2d commit cd15620

13 files changed

+174
-48
lines changed

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+
## 1.2.2
2+
3+
* Supports returning value in generated `push` method. [go_router CHANGELOG](https://github.com/flutter/packages/blob/main/packages/go_router/CHANGELOG.md#650)
4+
15
## 1.2.1
26

37
* Supports opt-in required extra parameters. [#117261](https://github.com/flutter/flutter/issues/117261)

packages/go_router_builder/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,17 @@ void _tap() => PersonRoute(pid: 'p1').go(context);
165165

166166
This is the point of typed routing: the error is found statically.
167167

168+
## Return value
169+
170+
Starting from `go_router` 6.5.0, pushing a route and subsequently popping it, can produce
171+
a return value. The generated routes also follow this functionality.
172+
173+
```dart
174+
void _tap() async {
175+
final result = await PersonRoute(pid: 'p1').go(context);
176+
}
177+
```
178+
168179
## Query parameters
169180

170181
Optional parameters (named or positional) indicate query parameters:

packages/go_router_builder/example/lib/all_types.g.dart

Lines changed: 13 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/go_router_builder/example/lib/extra_example.g.dart

Lines changed: 5 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/go_router_builder/example/lib/main.dart

Lines changed: 74 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ class App extends StatelessWidget {
7676
],
7777
),
7878
],
79-
)
79+
),
80+
TypedGoRoute<FamilyCountRoute>(path: 'family-count/:count'),
8081
],
8182
)
8283
class HomeRoute extends GoRouteData {
@@ -149,6 +150,17 @@ class PersonDetailsRoute extends GoRouteData {
149150
}
150151
}
151152

153+
class FamilyCountRoute extends GoRouteData {
154+
const FamilyCountRoute(this.count);
155+
156+
final int count;
157+
158+
@override
159+
Widget build(BuildContext context, GoRouterState state) => FamilyCountScreen(
160+
count: count,
161+
);
162+
}
163+
152164
class HomeScreen extends StatelessWidget {
153165
const HomeScreen({super.key});
154166

@@ -161,14 +173,38 @@ class HomeScreen extends StatelessWidget {
161173
title: const Text(App.title),
162174
centerTitle: true,
163175
actions: <Widget>[
164-
ElevatedButton(
165-
onPressed: () => const PersonRoute('f1', 1).push(context),
166-
child: const Text('Push a route'),
167-
),
168-
IconButton(
169-
onPressed: info.logout,
170-
tooltip: 'Logout: ${info.userName}',
171-
icon: const Icon(Icons.logout),
176+
PopupMenuButton<String>(
177+
itemBuilder: (BuildContext context) {
178+
return <PopupMenuItem<String>>[
179+
PopupMenuItem<String>(
180+
value: '1',
181+
child: const Text('Push w/o return value'),
182+
onTap: () => const PersonRoute('f1', 1).push(context),
183+
),
184+
PopupMenuItem<String>(
185+
value: '2',
186+
child: const Text('Push w/ return value'),
187+
onTap: () async {
188+
FamilyCountRoute(familyData.length)
189+
.push<int>(context)
190+
.then((int? value) {
191+
if (value != null) {
192+
ScaffoldMessenger.of(context).showSnackBar(
193+
SnackBar(
194+
content: Text('Age was: $value'),
195+
),
196+
);
197+
}
198+
});
199+
},
200+
),
201+
PopupMenuItem<String>(
202+
value: '3',
203+
child: Text('Logout: ${info.userName}'),
204+
onTap: () => info.logout(),
205+
),
206+
];
207+
},
172208
),
173209
],
174210
),
@@ -277,6 +313,35 @@ class PersonDetailsPage extends StatelessWidget {
277313
);
278314
}
279315

316+
class FamilyCountScreen extends StatelessWidget {
317+
const FamilyCountScreen({super.key, required this.count});
318+
319+
final int count;
320+
321+
@override
322+
Widget build(BuildContext context) => Scaffold(
323+
appBar: AppBar(title: const Text('Family Count')),
324+
body: Padding(
325+
padding: const EdgeInsets.all(16.0),
326+
child: Column(
327+
crossAxisAlignment: CrossAxisAlignment.stretch,
328+
children: <Widget>[
329+
Center(
330+
child: Text(
331+
'There are $count families',
332+
style: Theme.of(context).textTheme.headlineSmall,
333+
),
334+
),
335+
ElevatedButton(
336+
onPressed: () => context.pop(count),
337+
child: Text('Pop with return value $count'),
338+
),
339+
],
340+
),
341+
),
342+
);
343+
}
344+
280345
class LoginScreen extends StatelessWidget {
281346
const LoginScreen({this.from, super.key});
282347
final String? from;

packages/go_router_builder/example/lib/main.g.dart

Lines changed: 27 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/go_router_builder/example/lib/shell_route_example.g.dart

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)