This repository has been archived by the owner on Feb 25, 2022. It is now read-only.
GoRoute and GoRouteState may be generic #308
topperspal
started this conversation in
Ideas
Replies: 1 comment
-
I agree that such a thing would be nice. That's why we have a proposal: https://gorouter.dev/typed-routing. @kevmoo has it implemented in a PR: #246 |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
The simplest way to navigate to a page is
Navigator.of(context).push(MaterialPageRoute(builder: (context) => MyHomePage()));
. I know there is some simple methods like If you are using getx then u can simply navigate usingGet.to('MyHomePage()')
. This is also fine. In both of the above methods we are using dart's typed_language_feature . We can define as many parameters as we can either required or optional. This way Dart enforces us to provide specific arguments it needs to build the widget.But think about this:-
I have a
UserForm
that accepts an options argument of typeUser
. If theUser
is null then it will create a new user else it will update the old user. So using go_router, I can defineuserForm
route like this-and to navigate it I am using
This is not good, because I can also use
So you will see that it is not enforcing the routes to provide specific typed data. Here we loosing the
type_language_feature
of dart that is not a good practice.Suppose that I have registered 100 routes and one of the route is
and we still need to provide
extra
and somequeryParams
then how can I remember how many params I am using for a specific route and especially the order ofparams
. I can go to routes file and easily see whatparams
andqueryParams
it is accepting but that is annoying.How we can get rid of all these problems?
Here comes the
source_gen
package to rescue us. Here, I, for my own apps, build aTypedRoute
class asthen for each route, I create a relevent named constructor like this
Now I can easily navigate using
I create TypedRoute class manually but this can be generated using code generation. To use code generation
GoRoute
andGoRouteState
need to be changed slightly.GoRoute
may be generic andGoRouteState.extra
inference the type of object fromGoRoute
instead of hardcodedObject?
likeNow parse the route , extract the params,and generate some relevant code. This way you can generate named constructors in TypedData class for each and every route.
This also simplifies this
This make code more maintainable, readable, and very importantly less prone to errors because here we are using dart's
typed_language_feature
again. 😎I wish I can help you achieving this. But I can share TypedRouteClass for each and every scenario.
THANKS FOR READING IT CAREFULLY.
Beta Was this translation helpful? Give feedback.
All reactions