In Android’s MVVM passing context to viewmodel means breaking the pattern. Should you pass BuildContext to bloc class in Flutter’s Bloc architecture?
Is it ok to pass BuildContext to BLoC?
why would you do that? Generally, you don't want to do it. –
Ibnrushd
State of widgets always has one BuildContext associated to it and never changes it, so you can actually store it, but I don't think this is a good idea. Bloc class is VM in MVVM pattern and it shouldn't really know anything about View (widget tree).
No, it's not ok to pass BuildContext to BLoC. You can just stream or sinc your data between BLoC and your page if needed. For example, create an object of StreamController in BLoC and stream it on your page. BLoC and Widgets, each of them is in a separate layer of the architecture.
This is BLoC boilerplate. https://github.com/rvhamed/flutter_BLoC_boilerplate
© 2022 - 2024 — McMap. All rights reserved.