Riverpod
Arch
Notifiers
Intro
Notifiers are great, but confusing. In order to avoid problems, make sure to follow some best practices.
What they are
First of all though, what exactly are notifiers? In practice, they are stateful services, that provide a second state.
What does that mean? It means you have state, separate from your provided state. It's confusing, here is an example:
@riverpod
class MyController extends _$MyController {
// this is state
late SomeService _service;
// and this is your provided state
String build() => "some label";
// this method uses the notifier state, to update the provided state
void update() => state = _service.getNewLabel();
}
Testing
Unfortunately there is not a perfect solution right now, only an ok one. You have to include test code in production 🤮. Sorry.
@riverpod
class MyController extends _$MyController { ... }
class MyController extends _$MyController
with Mock
implements MyController {}
.
.
.