Flutterando / modular

A smart project structure
https://pub.dev/packages/flutter_modular
Other
1.3k stars 252 forks source link

BindNotFoundException thrown after trying to access a disposed cubit initialized with i.addLazySingleton #923

Open carloshpb opened 9 months ago

carloshpb commented 9 months ago

Describe the bug I went to a screen to change the current sub-account to another sub-account. To make it simpler to handle the logic of calling the new data on this sub-account, I've disposed the cubits which contains the data and immediately moved to the home screen that uses the disposed cubit.

Every cubit of the app is initialized inside the modules of imports :

class AppModule extends Module {
  @override
  List<Module> get imports => [
    CoreModule(),
    ...
  ]
  ...
}

All of them were initialized with i.addLazySingleton.

When I move to another screen which also uses a cubit that was disposed, I get the :

BindNotFoundException: UnregisteredInstance: SubScreenCubit unregistered

Still, the page ends up loading the cubit and working normally. Everything works normally and the application doesn't crash at all, but this BindNotFoundException is always being thrown and, sadly, it is being added to the Crashlytics.

That's all I can say about the bug. Sadly I can't show the code as its proprietary.

Environment Add your flutter doctor -v

Flutter 3.10.6 Dart 3.0.6 DevTools 2.23.1

Simulator OS versions: iOS 17 Android 12

Currently running on a macOS 14.1.2

Add your pubspec.yaml flutter_modular: ^6.3.2

To Reproduce Dispose a cubit that is lazySingleton and call it again. The error doesn't show on console at all. But its caught on crashlytics.

Expected behavior It is not supposed to throw BindNotFoundException, as the cubit was initialized again due to being a i.addLazySingleton.

jamesldr commented 8 months ago

Do you really need it to be a singleton? From your description since you're disposing it, I think adding it as a factory would work better.

caio-deiro commented 8 months ago

I had the same Issue, @carloshpb do you find a solution for this problem?

carloshpb commented 8 months ago

@caio-deiro I changed some codes... For example, I did more modules for inner children that are inside the parent module, which activate this bug. For now, doing this has solved my current situation. But the bug is still there. And it's weird, as it wasn't supposed to happen like this. But I also suspect the RouterOutlet may be the culprit as the Home module that I use to deal with the children makes use of a bottom navigation.