-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
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
[Question] Bloc observer not working #3022
Comments
Hi @linxkaa 👋 Can you remove the WidgetsFlutterBinding.ensureInitialized and verify if that solves the problem? If you aren’t accessing the underlying platforms before runApp then you should remove it. If you need access to the underlying platforms then you should use https://pub.dev/packages/flutter_services_binding. You can read more about this at flutter/flutter#94123. Let me know if that helps and sorry for any inconvenience! |
Hi @felangel thank you for responding! I already changed to this void main() {
BlocOverrides.runZoned(
() {},
blocObserver: SimpleBlocObserver(),
);
return runTokocuanApp(TokocuanEnv.dev);
} fyi the runApp i access in the code before isnt the actually runApp, i called other class like this void runTokocuanApp(String env) async {
WidgetsFlutterBinding.ensureInitialized();
configureInjection(env);
await SystemChrome.setPreferredOrientations(
[DeviceOrientation.portraitUp, DeviceOrientation.portraitDown],
);
return runApp(const TokocuanApp());
} Things i tried: I already tried moving the BlocOverrides to And i cant remove the await SystemChrome.setPreferredOrientations(
[DeviceOrientation.portraitUp, DeviceOrientation.portraitDown],
); |
void main() {
BlocOverrides.runZoned(
() => runTokocuanApp(…),
blocObserver: SimpleBlocObserver(),
);
} You need to move the code within the body of the zone. |
Ohh i see! Thank you very much! Its working now! |
Ive tried the above solution, but observer still doesn't work, no logs are showing |
Can you provide a link to a minimal reproduction sample? Thanks! |
|
This is because you're using If you replace it with You can see flutter/flutter#94123 for more info 👍 |
added plugin, changed to FlutterServicesBinding.ensureInitialized(); |
Can you also move the setup into the runZoned body? Future<void> main() async {
await BlocOverrides.runZoned(
() async {
await setUp();
return runApp(
BlocProvider<AuthenticationBloc>(
create: (_) => GetIt.instance<AuthenticationBloc>(),
child: MyApp(),
)
);
},
blocObserver: SimpleBlocObserver(),
);
} |
@felangel Now logs are visible, thanks a lot :) |
hello i put it in this way and it doesnt work void main() { ////FlutterServicesBinding.ensureInitialized(); } |
Can you share your full main.dart? |
this is my main import 'package:bloc/bloc.dart'; void main() { FlutterServicesBinding.ensureInitialized(); } class MyApp extends StatelessWidget { @OverRide
} |
i tried your both solution adding this FlutterServicesBinding.ensureInitialized(); and without and no result show |
Do you have a link to a minimal reproduction sample? |
and this my simple work link |
I'm not able to reproduce the issue. You can refer to the official DartPad and see the BlocObserver logs in the console: |
thank you it works actually i guess the problem was the ide is slow |
hello this is my main with no log observer if you can help Future main() async{ WidgetsFlutterBinding.ensureInitialized(); } |
@omarmorsi99 you need to move your WidgetsFlutterBinding and Firebase calls within the runZoned callback like: void main() {
BlocOverrides.runZoned(
() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);
runApp(const MyApp());
},
blocObserver: AppBlocObserver(),
);
} Hope that helps 👍 |
Hello! onError is not getting called from blocObserver for blocs of nested page. but it is getting called on my tab bar but if i navigate to a different page and that page has a blocProvider that is not printing any error. |
Hi @felangel. I tried to implement of blocObserver after the migration.
this is what my blocObserver looks like.
and this is my main.dart file
and it didn't print anything. Previously i do this with
Bloc.observer = SimpleBlocObserver()
and my bloc will automatically printed in console. But now it doesn't.if i put this this way:
with the
cubitMain()
codes like this:it will trigger only in the first time application is running, but not when the state is changed. While the previous version is whenever i create a bloc and trigger an event, the bloc observer should always be triggered.
Is there anything wrong with my code? Thank you in advance!
The text was updated successfully, but these errors were encountered: