yx_state 1.0.0 copy "yx_state: ^1.0.0" to clipboard
yx_state: ^1.0.0 copied to clipboard

A state management library for Dart/Flutter applications.

yx_state #

The yx_state package logo

A state management library for Dart/Flutter applications.

Pub Version


📦 Installation #

Add this package to your pubspec.yaml file:

dependencies:
  yx_state: <version>

🚀 Quick Start #

Basic Counter Example #

import 'package:yx_state/yx_state.dart';

// Define your state
class CounterState {
    final int count;

    const CounterState(this.count);

    @override
    bool operator ==(Object other) {
        if (identical(this, other)) return true;
        return other is CounterState && other.count == count;
    }

    @override
    int get hashCode => count.hashCode;
}

// Create a state manager
class CounterManager extends StateManager<CounterState> {
    CounterManager() : super(const CounterState(0));

    void increment() => handle((emit) async {
        emit(CounterState(state.count + 1));
      });

    void decrement() => handle((emit) async {
        emit(CounterState(state.count - 1));
      });
}

// Use the state manager
void main() {
    final counter = CounterManager();

    // Listen to state changes
    counter.stream.listen((state) {
        print('Count: ${state.count}');
    });

    // Trigger state changes
    counter.increment(); // Output: Count: 1
    counter.increment(); // Output: Count: 2
    counter.decrement(); // Output: Count: 1

    // Clean up resources when done
    counter.close();
}

🔧 Features #

State Observers #

Monitor state changes for debugging or analytics:

class MyCustomObserver extends StateManagerObserver {
  const MyCustomObserver();

  @override
  void onChange(
    StateManagerBase<Object?> stateManager,
    Object? currentState,
    Object? nextState,
    Object? identifier,
  ) {
    print(
      'State changed from $currentState to $nextState with '
      'identifier: $identifier',
    );
    super.onChange(stateManager, currentState, nextState, identifier);
  }
}

void main() {
  // Set the observer globally
  StateManagerOverrides.observer = const MyCustomObserver();
}

Global Overrides #

Customize behavior globally:

void main() {
    // Set the shouldEmit globally
    StateManagerOverrides.defaultShouldEmit = (current, next) => true;
}

Error Handling #

Comprehensive error handling with built-in support:

class ErrorHandlingStateManager extends StateManager<MyState> {
  ErrorHandlingStateManager() : super(MyState.initial());

  Future<void> performOperation() => handle((emit) async {
    try {
      ...
      // Risky operation
      final result = await someApiCall();
      emit(MyState.success(data: result));
    } catch (error, stackTrace) {
      // Report error through the state manager
      addError(error, stackTrace);
      ...
    }
  });
}
24
likes
160
points
35
downloads
screenshot

Publisher

verified publisherdev.go.yandex

Weekly Downloads

A state management library for Dart/Flutter applications.

Repository (GitHub)
View/report issues
Contributing

Topics

#yx-state #state-management

Documentation

API reference

License

MIT (license)

Dependencies

meta

More

Packages that depend on yx_state