modular icon indicating copy to clipboard operation
modular copied to clipboard

Properties lost in Singleton binding defined on shared module

Open agustin-garcia opened this issue 1 year ago • 1 comments

I'm defining a shared/code module by creating a Singleton binding but its properties become null when I call a child Module

In the initial (Login) module I can do: Modular.get<AuthenticationManager>().isLogged = true; print("isLogged: ${Modular.get<AuthenticationManager>().isLogged}"); and everything seems all right.

But as soon as I navigate to the HOME (or any other) module from the LOGIN module via the "Modular.to.pushReplacementNamed(AppPaths.home);" instruction, the AuthenticationManager properties become NULL again

Could you please look at the code snippet and try to find out what am I missing?

Thanks


class CoreModule extends Module { @override List get binds => [Bind.singleton((i) => AuthenticationManager(), export: true),]; }

class AppModule extends Module { @override List get imports => [CoreModule()]; @override List get routes => [ ChildRoute('/', child: (_, __) => const AppRoot(), guards: [AuthGuard()]), ModuleRoute(AppPaths.home, module: HomeModule()), ModuleRoute(AppPaths.login, module: LoginModule()), ]; }

class LoginModule extends Module { @override List get imports => [CoreModule()]; @override List get routes => [ChildRoute('/', child: (_, __) => LoginWidget()),]; }

class HomeModule extends Module { @override List get imports => [CoreModule()]; @override List get routes => [ChildRoute('/', child: (_, __) => const HomePage()),]; }

class AuthGuard extends RouteGuard {AuthGuard() : super(redirectTo: AppPaths.login); @override Future canActivate(String url, ModularRoute router) async { return Modular.get().isLogged; } }

FLUTTER • flutter_modular 5.0.3 • Flutter version 3.19.0 on channel stable • Upstream repository https://github.com/flutter/flutter.git • Framework revision bae5e49bc2 (6 days ago), 2024-02-13 17:46:18 -0800 • Engine revision 04817c99c9 • Dart version 3.3.0 • DevTools version 2.31.1

agustin-garcia avatar Feb 21 '24 20:02 agustin-garcia

@agustin-garcia, do it:

return Modular.get<AuthenticationManager>().isLogged;

edugemini avatar Feb 22 '24 13:02 edugemini