I have to build a single page that consists of let’s say a switch and a button. The user can continue to the next screen only if he has turned on the switch, then the user can press the button and go to the next screen. This is a quite simple case and I was wondering if using a Cubit here is an overkill or should I use StatefulWidget here?
- Why is better to use here a Cubit?
- Why is better to use here a StatefulWidget?
- Should I, in general, use StatefulWidget for simple cases like the above one?
2
Answers
I prefer to manage simple, well contained state (e.g. a bool in your case) in
State
rather than trying to reach for something more complicated like a Change/StateNotifier, Riverpod, BLoC, etc. If your states starts to span across multiple layers or screens, it might be time to start reaching for something a little more sophisticated. That could be one of the previously mentioned approaches or simply just parameters and callbacks.For a simple case like this, using a
StatefulWidget
would be sufficient. Using aCubit
might be overkill and can add unnecessary complexity to your code.However, using a
Cubit
in simple cases can promote the separation of concerns, make your code more testable and maintainable, and make it easier to add new features later on.