-
Notifications
You must be signed in to change notification settings - Fork 28.6k
[go_router_builder][go_router] .location
, .go(context)
and .push(context)
are only defined in the generated extension
#106790
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I'm willing to work on this by the way, but I guess this needs to be discussed/workshopped first |
I have a proposal. Let's say a class abstract class GoRouteDataImpl {
String get location;
void go(BuildContext context);
} It would define the methods that will be generated by the generator and it could be part of Then let's consider this set of routes (taken from the example in the doc): @TypedGoRoute<HomeRoute>(
path: '/',
routes: [
TypedGoRoute<FamilyRoute>(
path: 'family/:fid',
routes: [
TypedGoRoute<PersonRoute>(
path: 'person/:pid',
),
],
),
],
)
class HomeRoute extends GoRouteData {
const HomeRoute();
@override
Widget build(BuildContext context) => const HomeScreen();
}
class FamilyRoute extends GoRouteData {
const FamilyRoute({
required this.fid,
});
final String fid;
@override
Widget build(BuildContext context) => FamilyScreen(fid: fid);
}
class PersonRoute extends GoRouteData {
const PersonRoute({
required this.fid,
required this.pid,
});
final String fid;
final String pid;
@override
Widget build(BuildContext context) => PersonScreen(fid: fid, pid: pid);
} The generated file could look like this: final $appRoutes = <GoRoute>[
$homeRoute,
];
GoRoute get $homeRoute => GoRouteData.$route(
path: '/',
factory: GHomeRoute._fromState,
routes: [
GoRouteData.$route(
path: 'family/:fid',
factory: GFamilyRoute._fromState,
routes: [
GoRouteData.$route(
path: 'person/:pid',
factory: GPersonRoute._fromState,
),
],
),
],
);
class GHomeRoute extends HomeRoute implements GoRouteDataImpl {
const GHomeRoute();
factory GHomeRoute._fromState(GoRouterState state) => const GHomeRoute();
@override
String get location => GoRouteData.$location(
'/',
);
@override
void go(BuildContext context) {
GoRouter.of(context).go(location);
}
}
class GFamilyRoute extends FamilyRoute implements GoRouteDataImpl {
const GFamilyRoute({
required super.fid,
});
factory GFamilyRoute._fromState(GoRouterState state) => GFamilyRoute(
fid: state.params['fid']!,
);
@override
String get location => GoRouteData.$location(
'/family/${Uri.encodeComponent(fid)}',
);
@override
void go(BuildContext context) {
GoRouter.of(context).go(location);
}
}
class GPersonRoute extends PersonRoute implements GoRouteDataImpl {
const GPersonRoute({
required super.fid,
required super.pid,
});
factory GPersonRoute._fromState(GoRouterState state) => GPersonRoute(
fid: state.params['fid']!,
pid: state.params['pid']!,
);
@override
String get location => GoRouteData.$location(
'/family/${Uri.encodeComponent(fid)}/person/${Uri.encodeComponent(pid)}',
);
@override
void go(BuildContext context) {
GoRouter.of(context).go(location);
}
} The user would manipulate GFamilyRoute(fid: 'family0').go(context); And he could be able to implement some logic on void prindLocationAndGo(BuildContext context, GoRouteDataImpl route) {
print(route.location);
route.go(context);
} I have made an example of the generated file (written by hand) here: https://github.com/ValentinVignal/flutter_app_stable/tree/go-router/route-generation What do you think? |
Yes please, this proposal is actually important. Right now we cannot depend on Our use-case is that, we have a modular method that determines the route name to use as navigation destination. This method is used across multiple views:
|
Is there a workaround yet? |
Uh oh!
There was an error while loading. Please reload this page.
Use case
Right now, the generation creates an extension on each pages:
But because of that, when manipulating a
GoRouteData
object, we cannot usego
,push
orlocation
.This also prevents us from creating an extension on
GoRouteData
that reuses those methods:Proposal
It would be nice if the generated code doesn't work with extension (like what
freezed
does) for exampleThe text was updated successfully, but these errors were encountered: