value_state 2.0.0-beta.0 copy "value_state: ^2.0.0-beta.0" to clipboard
value_state: ^2.0.0-beta.0 copied to clipboard

A dart package that helps implements basic states for BLoC library

A dart package that helps to implement basic states initial, success and error.

pub package Test codecov License: MIT

Features #

This package helps you manage the different states your data can have in your app (like loading, success, or error). It makes your code cleaner and easier to understand, especially when dealing with things like network requests, storage loading or complex operations.

It provides a way to represent a value that can be in one of three states:

  • initial
  • success
  • failure

Usage #

Value #

Create a value:

// The value is in the initial state.
final valueInitial = Value<int>.initial();

// The value is in the success state with data.
final valueSuccess = Value.success(1);

print('Data of value : ${valueSuccess.data}'); // Data of value : 1

// Map a Value to `failure` with actual data if any.
// There is no `Value.failure` constructor to prevent developers from forgetting to retain the data from a previous state of the Value.
final valueError = value1.toFailure(Exception('error'));

print('Data of value : ${valueError.data}'); // Data of value : null
print('Error of value : "${valueError.error}"'); // Error of value : "Exception: error"

// The new value from call `toFailure` on `valueSuccess` keep previous `data`. It provides a simple way to display both error and previous data (for example a refresh failure).
final valueErrorWithPreviousData = valueSuccess.toFailure(Exception('error'));

print('Data of value : ${valueErrorWithPreviousData.data}'); // Data of value : 1
print('Error of value : "${valueErrorWithPreviousData.error}"'); // Error of value : "Exception: error"

Get the state of the value:

// Get the state of the value.
final state = valueInitial.state; // ValueState.initial

// Check if the value is in the initial state.
final isInitial = valueInitial.isInitial; // true

// Check if the value is in the success state.
final isSuccess = valueSuccess.isSuccess; // false

// Check if the value is in the failure state.
final isFailure = valueError.isFailure; // false

Map the value to a different type:

// Map the value to a different type.
final map = valueInitial.map(
  initial: () => 'initial',
  success: (data) => 'success: $data',
  failure: (error) => 'failure: $error',
  orElse: () => 'orElse',
);

When the value is in a specific state:

// When the value is in a specific state.
final when = value.when(
  initial: () => print('initial'),
  success: (data) => print('success: $data'),
  failure: (error) => print('failure: $error'),
  orElse: () => print('orElse'),
);

Merge two values with different types:

// Merge two values with different types.
final mergedValue = value1.merge<int>(value2, mapData: (value) => value.length);

Fetch #

Generate a stream of Value during a processing Future:

// Generate a stream of Value during a processing Future.
final stream = Future.value(1).toValues();

Handle states (isFetching, success, error...) while an action is processed:

// Handle states (isFetching, success, error...) while an action is processed.
final fetch = value.fetch(
  () async => 1,
);

License #

MIT License

See the LICENSE file for details.

Feedback #

Please file any issues, bugs or feature requests as an issue on the Github page.

5
likes
0
points
2.15k
downloads

Publisher

verified publisherdevobs.dev

Weekly Downloads

A dart package that helps implements basic states for BLoC library

Homepage
Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

meta, stream_transform

More

Packages that depend on value_state