auto_route_library
auto_route_library copied to clipboard
Can't replace route by same route with different param without replacing pages
Route path: '/booking/search/!/:departureCode/:arrivalCode/:date/:passengers'
.
I want to change the date so that the date changes in the url and the page keeps the state.
I do not want one route to be visually replaced by another, I just want to change the url.
Is it possible?
@MaterialAutoRouter(
replaceInRouteName: 'Screen,Route',
routes: <AutoRoute>[
AutoRoute(path: '/', page: HomeScreen, initial: true),
AutoRoute(
fullMatch: true,
path: '/booking/search/!/:departureCode/:arrivalCode/:date/:passengers',
page: FlightResultsOutboundScreen,
guards: [DirectionsGuard],
usesPathAsKey: true,
),
RedirectRoute(path: '*', redirectTo: '/'),
],
)
When usesPathAsKey: true
routes visually replace each other (unwanted behavior).
When usesPathAsKey: false
route visually stay same (desired behavior), guard onNavigation
fires and resolver.route.pathParams.get('date')
has updated date (desired behavior), but url has old date from previous route (unwanted behavior).
P.S. I only use guards (and auto_route_library) to restore the state of the website after the user has refreshed the page and to handle deep links for both mobile apps and the site.
Maybe this is the wrong way, so help me please.
up
@Milad-Akarie is there any solution for it? I had a same issue. I want to update my URL and change query parameters when some state changes in the same page.
Did you tye navigating to the same page with updated params? router.navigateNamed('current-path?query=q')
@Milad-Akarie This method does not work. Is there any other solution for it?
@payam-zahedi what happens?
@Milad-Akarie The Query parameter change in the widget and routeData, but the URL does not change. I can log them using
context.routeData.queryParams
and in widget
widget.id
But the URL is still the same.
Same here, when using navigateNamed()
to the same path with different query params, the @QueryParam
in the widget can get new value but the URL in the browser dose not updated.
İ can't reproduce this, please share an example.
Here is a simple example, the value has been changed but the URL remains the same.
@MaterialAutoRouter(
routes: <AutoRoute>[
AutoRoute(page: TestPage, path: '/page'),
],
)
class $AppRouter {}
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
final _router = AppRouter();
@override
Widget build(BuildContext context) {
return MaterialApp.router(
routerDelegate: _router
.delegate(initialRoutes: [TestPageRoute(value: 1)]), //auto_route
routeInformationParser: _router.defaultRouteParser(), //auto_r
);
}
}
class TestPage extends StatelessWidget {
final int? value;
const TestPage({Key? key, @queryParam this.value}) : super(key: key);
@override
Widget build(BuildContext context) {
return Center(
child: Column(
children: [
Text(value.toString()),
TextButton(
onPressed: () => AutoRouter.of(context)
.navigateNamed("/page?value=${(value ?? 1) + 1}"),
child: const Text("Next")),
],
),
);
}
}
https://user-images.githubusercontent.com/15973609/154660035-d6744518-d5da-4e7b-bc17-089eeed28ace.mov
@Milad-Akarie any update on this?
any update on this?
Have same problem.
It would be nice, if we could have a parameter to control if a page has to be created again, something like this:
AutoRouter.of(context).replace(const SamePageRoute(), recreate: true);
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions
I'm having the same issue. Any updates on this? Work-around that worked for me was to pop and then push the same route with different parameters again.
@Milad-Akarie I think replace should work instead
Any update on this?
Enabling usesPathAsKey and using replaceNamed() does not work.
[UPDATE]
I found this worked, but it just invoke build(context) method
so, we need to put any callback we want on that build method. Not a best practices but we can use it for a while
[UPDATE] I found this worked, but it just invoke
build(context) method
so, we need to put any callback we want on that build method. Not a best practices but we can use it for a while
That is very bad practice. So you are manually triggering the build function? Might as well do it on navigation which should be handled by the package.
Yes, it is. But we can use it until we have better options.
Not triggering it manually, when you navigate to the same route, build
function will be triggered automatically. So, we can use it to refresh our screen or do anything