result_notifier 0.1.0 result_notifier: ^0.1.0 copied to clipboard
“Pragmatic state management for Flutter, without the magic”
Result Notifier #
Pragmatic and magic-free state management for Flutter - simply lagom.
Result Notifier is a simple and modest package for state management, based on familiar and platform-native concepts, rather than introducing new abstractions and mental models. In fact, the package really is little more than a few additions to ValueNotifier and ChangeNotifier. As the name of this package alludes to, one of the most important additions is the concept of a Result type, which can represent either some Data, an Error or a Loading state.
Features #
- Familiar and platform-native concepts, based on
ValueNotifier
,ChangeNotifier
,ValueListenableBuilder
etc. - Built around a Result
type that represents some type of data. The result can be in one of three different states:
- Data: The result contains some concrete data.
- Error: Represents an error, along with the previous data, if any.
- Loading: Represents a loading/reloading state, along with the previous data, if any.
- The core class ResultNotifier -
a
ValueNotifier
that holds aResult
value and provides methods for accessing and mutating the value. - Support for updating the data asynchronously (e.g. via an API call) using FutureNotifier.
- Support for cache expiration and refreshing of data when stale.
- An easy way to build the UI based on the current state of the result, using ResultBuilder.
- An auto-disposable store of notifiers, each associated with a key (see ResultStore Makes it easy to support pagination or build a support a parameterised.
- A ResultNotifierProvider
that can be used to handle the lifecycle of a notifier (i.e. creation and disposal), and provide it to a subtree of
widgets.
- There is also ResourceProvider, which can manage the lifecycle and access to arbitrary resources.
Getting Started #
Simply add the dependency and start writing some code:
final notifier = ResultNotifier<String>.future(
(_) async {
final response = await http.get(Uri.parse('https://www.boredapi.com/api/activity/'));
final json = jsonDecode(response.body) as Map<String, dynamic>;
return json['activity'] as String;
},
);
You can find a more complete example here, and additional examples in the examples directory in the repository.
Or... rolling your own 🤷️ #
Instead of adding a dependency this package, consider building it yourself. It's really not that hard, especially since you can use the source code of this package as a starting point, and just throw out the parts you don't need/like.
When to use it - and when not to #
Result Notifier is probably most suitable for cases when your state management needs are in the ballpark of "low to moderate", or as we say in Sweden: lagom. If you need more advanced state management, you might want to reach for something more elaborate. But then again, maybe not - as in most cases, this very much depends on your general application architecture and modularization. And remember - excessive use of state management may also be a sign of a flawed architecture or over-engineering.
Things left to do... #
The usual stuff, more tests and more docs 😅.