auto_route_library icon indicating copy to clipboard operation
auto_route_library copied to clipboard

Can't replace route by same route with different param without replacing pages

Open adimshev opened this issue 3 years ago • 12 comments

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.

adimshev avatar Nov 25 '21 23:11 adimshev

up

rbarbosas avatar Jan 27 '22 20:01 rbarbosas

@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.

payam-zahedi avatar Feb 06 '22 14:02 payam-zahedi

Did you tye navigating to the same page with updated params? router.navigateNamed('current-path?query=q')

Milad-Akarie avatar Feb 06 '22 14:02 Milad-Akarie

@Milad-Akarie This method does not work. Is there any other solution for it?

payam-zahedi avatar Feb 07 '22 11:02 payam-zahedi

@payam-zahedi what happens?

Milad-Akarie avatar Feb 07 '22 12:02 Milad-Akarie

@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.

payam-zahedi avatar Feb 07 '22 12:02 payam-zahedi

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.

MiaYi avatar Feb 18 '22 00:02 MiaYi

İ can't reproduce this, please share an example.

Milad-Akarie avatar Feb 18 '22 08:02 Milad-Akarie

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

MiaYi avatar Feb 18 '22 09:02 MiaYi

@Milad-Akarie any update on this?

payam-zahedi avatar May 15 '22 11:05 payam-zahedi

any update on this?

isurugg avatar Jun 01 '22 11:06 isurugg

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);

RUSSCITY avatar Jul 06 '22 13:07 RUSSCITY

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

github-actions[bot] avatar Sep 05 '22 08:09 github-actions[bot]

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

pshastov avatar Nov 06 '22 12:11 pshastov

Any update on this?

ariefwijaya avatar Nov 07 '22 06:11 ariefwijaya

Enabling usesPathAsKey and using replaceNamed() does not work.

mugikhan avatar Nov 09 '22 22:11 mugikhan

[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

ariefwijaya avatar Nov 09 '22 22:11 ariefwijaya

[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.

mugikhan avatar Nov 09 '22 23:11 mugikhan

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

ariefwijaya avatar Nov 09 '22 23:11 ariefwijaya