auto_route_library icon indicating copy to clipboard operation
auto_route_library copied to clipboard

Fire custom events on route open / close

Open subzero911 opened this issue 1 year ago • 3 comments

Idea is taken from GetX. It has "Bindings". It can register dependencies on route open and unregister on route close. I'm not the fan of GetX, but I like the particular idea. Instead of having the bloated "get_it registrar", it's better to bind dependencies only to those screens which actually need it. And on route close it will be disposed, saving some RAM...

AutoRoute(
        page: BookListPage(),
        onInit: () => GetIt.I.registerSingleton<IBookController>(BookController());
        onDispose: () => GetIt.I.unregister<IBookController>(disposingFunction: (controller) => controller.dispose());
),

It's clean - you can open the routes.dart file and easily figure out, which screen uses dependencies. And it gives you scopes - this controller could be read from all the child routes. Moreover, with such callbacks you can fire any custom events, not only GetIt.

I proposed the same thing for go_router, https://github.com/flutter/flutter/issues/99113, they liked the idea but didn't make it yet (issue hangs open since February).

subzero911 avatar Jul 08 '22 09:07 subzero911

Hello @subzero911 I really love the idea, but in my case I can't just pass a callback function they way you proposed because here we're in the static realm. I'll try to figure something out thu

Milad-Akarie avatar Jul 12 '22 10:07 Milad-Akarie

I achieve what you want by creating a listener to the router context. context.router.addListener(checkBeingActivatedRoute);

  Future<void> checkBeingActivatedRoute() async {
    final activeRoute =
        RoutesCore.retrieveContextListenerBeingActivatedRoute(context);
    switch(activeRoute){
    ....
    }
  }

JeanPSF avatar Jul 15 '22 19:07 JeanPSF

I need the same functionality! Any update?

eliasjtg avatar Sep 15 '22 22:09 eliasjtg

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 Nov 21 '22 08:11 github-actions[bot]

Still would love to see this feature.

subzero911 avatar Nov 21 '22 08:11 subzero911