Explanation and question about "Type-safe routes" and if there are plans to implement them #159
-
I was reading a Besides the above question, I have 5 more question about type-safe routes..
Disclaimer: This question is not only about |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Type safety is the idea that the compiler catches your mistakes instead of waiting until run-time. For example, if you have the following route: GoRouter(path: '/family/:fid', ...) and navigate to it w/o the 'fid' parameter: context.go('/family'); // oops. forgot the fid param this code will compile just fine. You won't see the exception until run-time because you should've called it like so: context.go('/family', params: {'fid': 'f2'}); However, if this was a typesafe route, you could imagine calling it like so: familyRoute.go(context, fid: 'f2'); In this case, the compiler would let you know if you forgot to add the The idea of using a code generator in Dart is to be able to take some data, like the list of routes, and turn that into a set of types w/o having to write that code yourself. The auto_route_generator takes the definition of routes and provides the types to enable typesafe routing. Obviously you don't need type safe routes to enable routing (since go_router has lots of happy customers), but it would be nice to have as an optional add-on. We've been working on such a thing for go_router, but we don't have it yet. |
Beta Was this translation helpful? Give feedback.
Type safety is the idea that the compiler catches your mistakes instead of waiting until run-time. For example, if you have the following route:
and navigate to it w/o the 'fid' parameter:
this code will compile just fine. You won't see the exception until run-time because you should've called it like so:
However, if this was a typesafe route, you could imagine calling it like so:
In this case, the compiler would let you know if you forgot to add the
fid
parameter.The idea of using a code generator in Dart is to be able…