Is it ok to pass BuildContext to BLoC?
Asked Answered
G

2

6

In Android’s MVVM passing context to viewmodel means breaking the pattern. Should you pass BuildContext to bloc class in Flutter’s Bloc architecture?

Galarza answered 22/10, 2019 at 13:26 Comment(1)
why would you do that? Generally, you don't want to do it.Ibnrushd
M
3

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).

Manicdepressive answered 22/10, 2019 at 13:48 Comment(0)
K
0

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

Kamenskuralski answered 22/10, 2019 at 13:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.