mayr_stateman 1.0.0
mayr_stateman: ^1.0.0 copied to clipboard
A lightweight and intuitive state utility for Dart. Easily manage and track states, statuses, messages, and payloads with simple, chainable methods.
π¦ Mayr StateMan #
A lightweight and intuitive state utility for Dart.

mayr_stateman provides an easy way to manage and track the status of any operation.
Whether loading, updating, succeeding, or failing, mayr_stateman keeps your state transitions clean and predictable β without heavy dependencies or boilerplate.
β¨ Features #
-
Simple & Lightweight: Pure Dart state management with zero dependencies.
-
Comprehensive Status Management: Handle
idle,loading,success,error,updating, andemptystates effortlessly. -
Fluent API: Chain multiple state updates elegantly for cleaner, more readable code.
-
Type-Safe Payloads: Built-in support for attaching type-safe payloads to your states.
-
Message Support: Associate messages with state transitions for better debugging and UX.
-
Intuitive Getters: Convenient getters like
isLoading,isSuccessWithPayload,hasValidPayload,isProcessing, and more. -
Visual Feedback: Emoji representations for each status β perfect for debugging and UI feedback.
-
Versatile: Designed for both frontend UI state and backend logic scenarios.
-
Well-Tested: Comprehensive test coverage ensuring reliability and stability.
-
Production Ready: v1.0.0 follows software engineering best practices (KISS, DRY, SRP, SOC).
π Getting started #
-
Add
mayr_statemanto yourpubspec.yaml:dependencies: mayr_stateman: # check for the latest version on pub.dev -
Install the package:
flutter pub get -
Import it into your Dart file:
import 'package:mayr_stateman/mayr_stateman.dart';
Alternatively, you could install it using the command
flutter pub add mayr_stateman
Usage #
Basic Usage #
To use MayrStateman, simply create an instance with your desired payload type:
// Generic stateman - no specific payload type
MayrStateman state = MayrStateman.init();
// Stateman with String payload
MayrStateman<String> state = MayrStateman<String>.init();
// Stateman with custom object payload
MayrStateman<User> userState = MayrStateman<User>.init();
The MayrStateman class is generic, allowing you to define a type-safe payload for your state manager.
Setting and Getting State #
You can easily manage the state with various setter methods and retrieve the current state with getter methods:
MayrStateman<String> state = MayrStateman<String>.init();
// Setting state to loading
state.setStatusLoading();
// Setting a message
state.setMessage('Fetching data...');
// Setting state to success with a payload
state.setSuccessWithPayload('Success!');
// Checking if state is success with valid payload
if (state.isSuccessWithPayload) {
print('Payload: ${state.payload}');
}
Method Chaining #
One of the powerful features of MayrStateman is method chaining:
state
.setStatusLoading()
.setMessage('Fetching user data...')
.setPayload(null);
// After async operation
state
.setPayload(userData)
.setStatusSuccess()
.setMessage('Data loaded successfully');
Resetting the State #
You can reset the state back to idle with:
state.reset();
This resets the state to idle, clears any message, and removes the payload.
Practical Example #
Here's a real-world example of using MayrStateman in an async operation:
class UserRepository {
final MayrStateman<User> userState = MayrStateman<User>.init();
Future<void> fetchUser(String userId) async {
// Set loading state
userState.setStatusLoading().setMessage('Fetching user...');
try {
// Simulate API call
final user = await api.getUser(userId);
// Set success state with payload
userState.setSuccessWithPayload(user);
} catch (e) {
// Set error state with message
userState.setErrorWithMessage('Failed to fetch user: $e');
}
}
}
// In your UI
if (userState.isLoading) {
return CircularProgressIndicator();
}
if (userState.isError) {
return Text(userState.message ?? 'An error occurred');
}
if (userState.isSuccessWithPayload) {
return UserProfile(user: userState.payload!);
}
return Text('No data');
Using the Emoji Getter for State #
For a more user-friendly experience, you can easily access emojis representing the current state. For example:
MayrStateman<String> state = MayrStateman<String>.init();
// Setting the state to error
state.setErrorWithMessage('An error occurred');
// Accessing emoji for the current state
print('Current State Emoji: ${state.statusEmoji}');
// This would output:
// Current State Emoji: β
Cheetsheet #
Getters #

Setters #

MayrStatus #
The package also exposes the status enum it uses.
enum MayrStatus { empty, error, idle, loading, success, updating }
The status can be accessed from the stateman using
state.status;
And it exposes an status.emoji property.
| Status | Emoji |
|---|---|
MayrStatus.empty |
π |
MayrStatus.error |
β |
MayrStatus.idle |
βΈοΈ |
MayrStatus.loading |
β³ |
MayrStatus.success |
β |
MayrStatus.updating |
π |
π’ Additional Information #
π€ Contributing #
Contributions are highly welcome! If you have ideas for new extensions, improvements, or fixes, feel free to fork the repository and submit a pull request.
Please make sure to:
- Follow the existing coding style.
- Write tests for new features.
- Update documentation if necessary.
Let's build something amazing together!
π Reporting Issues #
If you encounter a bug, unexpected behaviour, or have feature requests:
- Open an issue on the repository.
- Provide a clear description and steps to reproduce (if it's a bug).
- Suggest improvements if you have any ideas.
Your feedback helps make the package better for everyone!
π Licence #
This package is licensed under the MIT License β which means you are free to use it for commercial and non-commercial projects, with proper attribution.
See the LICENSE file for more details.
π Support #
If you find this package helpful, please consider giving it a βοΈ on GitHub β it motivates and helps the project grow!
You can also support by:
- Sharing the package with your friends, colleagues, and tech communities.
- Using it in your projects and giving feedback.
- Contributing new ideas, features, or improvements.
Every little bit of support counts! ππ
