Skip to main content

Why Code on the Rocks?

Code on the Rocks (CotR) is a straightforward MVVM state management package that aims to make adopting and mastering Flutter a breeze. It was designed to be easy to learn, easy to use, and easy to transition away from if you'd like.

The goal of CotR is to stick as close to the Flutter framework as possible so that everything you learn is directly applicable to every other Flutter app you work on. With a little bit of practice, you should feel confident enough to remove all state management packages from your pubspec.yaml (yes, even this one ๐Ÿ˜ญ).

This package consists of 3 classes that each extend a core Flutter class:

CotRFlutterMVVM
ViewModelBuilderStatefulWidgetView
ViewModelStateViewModel
ViewModelProviderInheritedWidget-

The CotR classes abstract away some of the boilerplate required to implement this specific flavor of MVVM but you can implement it in pure Flutter with a little extra code.

If you take one thing away from implementing this package, it should be that Flutter by itself is more than capable of managing your entire app state.

Another State Management Solution?โ€‹

If you've been using Flutter for any amount of time, you're probably aware of the large list of state management packages that exist. I've listed the most popular ones below with some arbitrary complexity metrics.

Package# of Classes# of Dependencies
flutter_riverpod1064
get960
stacked847
states_rebuilder534
provider462
flutter_bloc262
mvc_pattern101
flutter_mobx61
flutter_redux31
scoped_model30

I find complexity in a state management solution unnecessary, especially considering the inherent power of Flutter. Why add more when you can get by with less? With that in mind, I wanted to create a lightweight solution that was simple and, of course, different than what was already available.

Isn't this ScopedModel?โ€‹

Of all the currently available state management packages, code_on_the_rocks is most similar to scoped_model, but there are a few differences.

  • scoped_model Models are ChangeNotifiers. CotR ViewModels are State objects.
  • In scoped_model, ScopedModels use AnimationBuilders to listen to their Models. In CotR, ViewModelBuilders are StatefulWidget that get rebuilt when setState is called.
  • ScopedModels require the Model to be passed in as an argument. A ViewModelBuilder is unique to a ViewModel and does not require an argument.
  • ViewModels in CotR can override the initState and dispose methods of the State class.

Inspirationโ€‹

Over the years I've become a big fan of several different "state-management" solutions:

  • InheritedWidgets are powerful widgets built into the Flutter Framework
  • Stacked is an opinionated MVVM library that uses ViewModels, ViewModelBuilders, and ViewModelWidgets
  • get_it lets you easily access services from anywhere in your app
  • get_it_mixin lets you bind views to services stored in GetIt

This package is an attempt to combine the best parts of these solutions into a single package that is easy to use and understand.