Bloc event doesn't trigger mapEventToState after leaving the page where the BlocProvider is instantieted and then comming back. I created this repo below to reproduce the bug.
https://github.com/Mr-Martini/boolean_changer.git
Steps to reproduce the behavior:
Expected behavior
It should still works after leaving the page and then commig back.
*Logs *
No logs at all
[√] Flutter (Channel master, 1.22.0-10.0.pre.196, on Microsoft Windows [Version 10.0.19041.508], locale en-US)
• Flutter version 1.22.0-10.0.pre.196 at C:\src\flutter
• Framework revision ea039ed3f9 (15 hours ago), 2020-09-15 17:57:04 -0400
• Engine revision 1cabedf8ac
• Dart version 2.10.0 (build 2.10.0-124.0.dev)
[√] Android toolchain - develop for Android devices (Android SDK version 30.0.2)
• Android SDK at C:\Users\marcoAppData\LocalAndroid\sdk
• Platform android-30, build-tools 30.0.2
• Java binary at: C:\Program FilesAndroidAndroid Studio\jrebin\java
• Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b01)
• All Android licenses accepted.
[√] Android Studio (version 4.0)
• Android Studio at C:\Program FilesAndroidAndroid Studio
• Flutter plugin version 48.1.2
• Dart plugin version 193.7547
• Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b01)
[√] VS Code (version 1.49.0)
• VS Code at C:\Users\marcoAppData\Local\Programs\Microsoft VS Code
• Flutter extension version 3.14.1
[√] Connected device (1 available)
• sdk gphone x86 arm (mobile) • emulator-5554 • android-x86 • Android 11 (API 30) (emulator)
• No issues found!
same issue here ,mine returned null after coming back to the page I left from.(Does this have to do with bloc not being disposed because the screen wasn't popped from the stack) although I have checked for all my states.
sometimes the next page is create twice
Hi 👋 @Mr-Martini, I checked your code and I found that you are registering the BLoC as a singleton class using getIt. What's happening is when you first open the SecondPage a new instance will be provided from getIt and when you close that page the bloc will be disposed automatically and when you try to open the SecondPage again you will get the same instance as the old BLoC from getIt because it is registered as a singleton, but it cannot be used anymore because it was disposed when you open and exit the SecondPage at the first time .
The Fix:
Go to injection.dart file and replace this code
sl.registerLazySingleton(
() => BooleanBloc(
useCase: sl(),
),
);
by
sl.registerFactory(
() => BooleanBloc(
useCase: sl(),
),
);
Permanent Fix:
Don't use singletons for BLoCs unless you are sure it is not going to be needed once it is disposed.
@Elias8 Hi, thanks for your reply. It is working as expected now.
Most helpful comment
Hi 👋 @Mr-Martini, I checked your code and I found that you are registering the BLoC as a singleton class using
getIt. What's happening is when you first open theSecondPagea new instance will be provided fromgetItand when you close that page the bloc will be disposed automatically and when you try to open theSecondPageagain you will get the same instance as the old BLoC fromgetItbecause it is registered as a singleton, but it cannot be used anymore because it was disposed when you open and exit theSecondPageat the first time .The Fix:
Go to injection.dart file and replace this code
by
Permanent Fix:
Don't use singletons for BLoCs unless you are sure it is not going to be needed once it is disposed.