mayr_stateman 1.0.0 copy "mayr_stateman: ^1.0.0" to clipboard
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.

License Platform

Pub Version Pub.dev Score Pub Likes Pub.dev Publisher Downloads

Build Status Issues Last Commit Contributors

🚦 Mayr StateMan #

A lightweight and intuitive state utility for Dart. quick-usage.png

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, and empty states 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 #

  1. Add mayr_stateman to your pubspec.yaml:

    dependencies:
        mayr_stateman: # check for the latest version on pub.dev
    
  2. Install the package:

    flutter pub get
    
  3. 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 #

Getters.png

Setters #

Setters.png

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! πŸš€πŸ’™

1
likes
160
points
120
downloads
screenshot

Publisher

verified publishermayrlabs.com

Weekly Downloads

A lightweight and intuitive state utility for Dart. Easily manage and track states, statuses, messages, and payloads with simple, chainable methods.

Repository (GitHub)
View/report issues

Topics

#state #utilities #dart #productivity #toolkit

Documentation

Documentation
API reference

License

MIT (license)

More

Packages that depend on mayr_stateman