Milad-Akarie / auto_route_library

Flutter route generator

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Fire custom events on route open / close

subzero911 opened this issue · 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, flutter/flutter#99113, they liked the idea but didn't make it yet (issue hangs open since February).

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

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){
    ....
    }
  }

I need the same functionality! Any update?

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

Still would love to see this feature.

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 need the same functionality! Any update?

@eliasjtg you can implement AutoRouteWrapper, and add your custom onInit/onDispose callbacks, called in initState() and dispose()
https://pub.dev/packages/auto_route#wrapping-routes