-
Notifications
You must be signed in to change notification settings - Fork 259
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
class that bind with singleton in core shared module should not recreate #794
Comments
Show more parts of your code so we can reproduce the issue |
I'm having the same issue with Part of my code: void main() async {
runApp(
ModularApp(
module: AppModule(),
child: const AppWidget(),
),
);
await Hive.initFlutter();
await Modular.get<CacheLoginService>().openBox();
await Modular.get<AuthController>().loadCachedUser();
await Future.delayed(const Duration(seconds: 5));
Modular.to.navigate('/login/');
} class CoreModule extends Module {
@override
void exportedBinds(i) {
i.addInstance(Dio());
i.addLazySingleton<IClientHttp>(ClapsClientApi.new);
i.addLazySingleton<AuthService>(AuthService.new);
i.addLazySingleton<AuthController>(AuthController.new);
i.addLazySingleton<EncryptedDBService>(EncryptedDBService.new);
i.addLazySingleton<CacheLoginService>(CacheLoginService.new);
}
} class AppModule extends Module {
@override
List<Module> get imports => [CoreModule()];
@override
void routes(r) {
...
}
} class LoginModule extends Module {
@override
List<Module> get imports => [CoreModule()];
@override
void binds(i) {
// new dependencies different from CoreModule
...
}
@override
void routes(r) {
...
}
} Screenshot notes:
|
Complete code to help test the issue. import 'package:flutter/material.dart';
import 'package:flutter_modular/flutter_modular.dart';
void main() async {
runApp(ModularApp(module: AppModule(), child: const AppWidget()));
await Future.delayed(const Duration(seconds: 2));
Modular.to.navigate('/auth/');
}
class Counter {
int count = 0;
Counter() {
print('****INSTANCE: $runtimeType - $hashCode');
}
void add() {
count++;
}
}
class CoreModule extends Module {
@override
void exportedBinds(Injector i) {
i.addSingleton<AuthService>(AuthServiceDefault.new);
i.addSingleton(Counter.new);
}
}
class AppModule extends Module {
@override
List<Module> get imports => [CoreModule()];
@override
void routes(RouteManager r) {
r.child('/', child: (context) => const SplashScreen());
r.module('/auth', module: AuthModule());
r.module('/home', module: HomeModule());
}
}
class AppWidget extends StatelessWidget {
const AppWidget({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp.router(
routerConfig: Modular.routerConfig,
);
}
}
class SplashScreen extends StatelessWidget {
const SplashScreen({super.key});
@override
Widget build(BuildContext context) {
return const Material(child: Center(child: CircularProgressIndicator.adaptive()));
}
}
// Auth
class AuthModule extends Module {
@override
List<Module> get imports => [CoreModule()];
@override
void binds(Injector i) {
i.addLazySingleton(AuthController.new);
}
@override
void routes(RouteManager r) {
r.child('/', child: (context) => LoginPage(authController: Modular.get()));
}
}
abstract interface class AuthService {
Future<bool> login();
}
class AuthServiceDefault implements AuthService {
AuthServiceDefault() {
print('***INSTANCE: $runtimeType - $hashCode');
}
@override
Future<bool> login() async {
await Future.delayed(const Duration(seconds: 1));
return true;
}
}
class AuthController {
AuthController({required this.authService});
final AuthService authService;
Future<bool> login() async {
return authService.login();
}
}
class LoginPage extends StatelessWidget {
const LoginPage({super.key, required this.authController});
final AuthController authController;
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: ElevatedButton(
onPressed: () async {
final counter = Modular.get<Counter>();
counter.add();
counter.add();
counter.add();
await authController.login();
Modular.to.navigate('/home/');
},
child: const Text('Login'),
),
),
);
}
}
// Home
class HomeModule extends Module {
@override
List<Module> get imports => [CoreModule()];
@override
void routes(RouteManager r) {
r.child('/', child: (context) => HomePage(counter: Modular.get()));
}
}
class HomePage extends StatelessWidget {
const HomePage({super.key, required this.counter});
final Counter counter;
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(child: Text('Home ${counter.count} ${counter.hashCode}')),
);
}
}
|
Hi @guilherme-ma9, In Modular 6.3.1 the problem with |
Hi @eduardoflorence, The problem still remains. |
@guilherme-ma9, Can you provide a complete code for us to reproduce the problem? Or are you able to change the code I provided above to reproduce the problem? |
@guilherme-ma9 try to log your binds instead your imported modules. |
It's working normally now |
Hello, I tried with both: and @OverRide I can't migrate to versino 6 because many dependencies my project has. |
@agustin-garcia, in version 5: @override
List<Bind> get binds => [
Bind.singleton((i) => AuthenticationManager(), export: true),
]; |
Hello, class CoreModule extends Module { class AppModule extends Module { class LoginModule extends Module { @OverRide Then in the LoginWidget, I call it like: The setting of properties work while in the same module, but if I move to another module, the Object's properties are all null. |
Use @override
List<Bind> get binds => [
Bind.singleton((i) => AuthenticationManager(), export: true),
Bind.singleton((i) => StorageSettingsController(), export: true),
]; |
Thanks Eduardo, But as soon as I navigate to the HOME 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 again ` class AppModule extends Module { class LoginModule extends Module { class HomeModule extends Module { class AuthGuard extends RouteGuard { |
Any guidance on this or where else to look for it? |
@agustin-garcia, do it: Modular.get<AuthenticationManager>().isLogged |
We need as described in docs.
class AppModule extends Module {
@OverRide
List get imports => [CoreModule()];
and inside core module injecting Dio instance as in examples
but when getting this httclient via Modular.get() the dio instance class construction called again.
so what is should we do in order to get a real singleton binding ?
The text was updated successfully, but these errors were encountered: