Skip to content

Commit 97fe921

Browse files
authored
[go_router] Add routing functions to GoRouteData (#9277)
second step of fixing [#106790](flutter/flutter#106790). [First PR](#9275) This PR adds the routing methods `.location`, `.go(context)`, `.push(context)`, `.pushReplacement(context)`, and `replace(context)` to `GoRouteData`. They will be overridden by the mixin generated by `go_router_builder` ## Pre-Review Checklist [^1]: Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling.
1 parent b90f8a7 commit 97fe921

File tree

4 files changed

+100
-1
lines changed

4 files changed

+100
-1
lines changed

packages/go_router/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1+
## 15.2.0
2+
3+
- `GoRouteData` now defines `.location`, `.go(context)`, `.push(context)`, `.pushReplacement(context)`, and `replace(context)` to be used for [Type-safe routing](https://pub.dev/documentation/go_router/latest/topics/Type-safe%20routes-topic.html). **Requires go_router_builder >= 3.0.0**.
4+
15
## 15.1.3
26

37
* Updates minimum supported SDK version to Flutter 3.27/Dart 3.6.
48
* Fixes typo in API docs.
59

10+
611
## 15.1.2
712

813
- Fixes focus request propagation from `GoRouter` to `Navigator` by properly handling the `requestFocus` parameter.

packages/go_router/lib/src/route_data.dart

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,30 @@ abstract class GoRouteData extends RouteData {
133133
static final Expando<GoRouteData> _stateObjectExpando = Expando<GoRouteData>(
134134
'GoRouteState to GoRouteData expando',
135135
);
136+
137+
/// The location of this route.
138+
String get location => throw _shouldBeGeneratedError;
139+
140+
/// Navigate to the route.
141+
void go(BuildContext context) => throw _shouldBeGeneratedError;
142+
143+
/// Push the route onto the page stack.
144+
Future<T?> push<T>(BuildContext context) => throw _shouldBeGeneratedError;
145+
146+
/// Replaces the top-most page of the page stack with the route.
147+
void pushReplacement(BuildContext context) => throw _shouldBeGeneratedError;
148+
149+
/// Replaces the top-most page of the page stack with the route but treats
150+
/// it as the same page.
151+
///
152+
/// The page key will be reused. This will preserve the state and not run any
153+
/// page animation.
154+
///
155+
void replace(BuildContext context) => throw _shouldBeGeneratedError;
156+
157+
static UnimplementedError get _shouldBeGeneratedError => UnimplementedError(
158+
'Should be generated using [Type-safe routing](https://pub.dev/documentation/go_router/latest/topics/Type-safe%20routes-topic.html).',
159+
);
136160
}
137161

138162
/// A class to represent a [ShellRoute] in

packages/go_router/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name: go_router
22
description: A declarative router for Flutter based on Navigation 2 supporting
33
deep linking, data-driven routes and more
4-
version: 15.1.3
4+
version: 15.2.0
55
repository: https://github.com/flutter/packages/tree/main/packages/go_router
66
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+go_router%22
77

packages/go_router/test/route_data_test.dart

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,76 @@ void main() {
280280
expect(routeWithDefaultCaseSensitivity.caseSensitive, false);
281281
},
282282
);
283+
284+
testWidgets(
285+
'It should throw beacuase there is no code generated',
286+
(WidgetTester tester) async {
287+
final List<FlutterErrorDetails> errors = <FlutterErrorDetails>[];
288+
289+
FlutterError.onError =
290+
(FlutterErrorDetails details) => errors.add(details);
291+
292+
const String errorText = 'Should be generated';
293+
294+
Widget buildWidget(void Function(BuildContext) onTap) {
295+
return MaterialApp(
296+
home: Builder(
297+
builder: (BuildContext context) => GestureDetector(
298+
child: const Text('Tap'),
299+
onTap: () => onTap(context),
300+
),
301+
),
302+
);
303+
}
304+
305+
final Widget pushThrower = buildWidget((BuildContext context) {
306+
const _GoRouteDataBuild().push<void>(context);
307+
});
308+
await tester.pumpWidget(pushThrower);
309+
await tester.tap(find.text('Tap'));
310+
311+
expect(errors.first.exception, isA<UnimplementedError>());
312+
expect(errors.first.exception.toString(), contains(errorText));
313+
314+
errors.clear();
315+
316+
final Widget goThrower = buildWidget((BuildContext context) {
317+
const _GoRouteDataBuild().go(context);
318+
});
319+
await tester.pumpWidget(goThrower);
320+
await tester.tap(find.text('Tap'));
321+
322+
expect(errors.first.exception, isA<UnimplementedError>());
323+
expect(errors.first.exception.toString(), contains(errorText));
324+
325+
errors.clear();
326+
327+
final Widget pushReplacementThrower =
328+
buildWidget((BuildContext context) {
329+
const _GoRouteDataBuild().pushReplacement(context);
330+
});
331+
await tester.pumpWidget(pushReplacementThrower);
332+
await tester.tap(find.text('Tap'));
333+
334+
expect(errors.first.exception, isA<UnimplementedError>());
335+
expect(errors.first.exception.toString(), contains(errorText));
336+
337+
errors.clear();
338+
339+
final Widget replaceThrower = buildWidget((BuildContext context) {
340+
const _GoRouteDataBuild().pushReplacement(context);
341+
});
342+
await tester.pumpWidget(replaceThrower);
343+
await tester.tap(find.text('Tap'));
344+
345+
expect(errors.first.exception, isA<UnimplementedError>());
346+
expect(errors.first.exception.toString(), contains(errorText));
347+
348+
errors.clear();
349+
350+
FlutterError.onError = FlutterError.dumpErrorToConsole;
351+
},
352+
);
283353
});
284354

285355
group('ShellRouteData', () {

0 commit comments

Comments
 (0)