这是indexloc提供的服务,不要输入任何密码

turbo_response 0.2.6 copy "turbo_response: ^0.2.6" to clipboard
turbo_response: ^0.2.6 copied to clipboard

A type-safe response wrapper for handling success and failure states with pattern matching and utility methods.

TurboResponse #

A type-safe response that can be either successful or failed, with proper error handling and pattern matching. Works seamlessly with both pure Dart and Flutter projects.

Features #

  • 🎯 Type-safe success and failure states
  • 🔄 Pattern matching with when and maybeWhen
  • 🛠️ Transformation methods like mapSuccess, mapFail, and andThen
  • ⚡ Async operation support
  • 🎁 Utility methods like unwrap, unwrapOr, and ensure
  • 🔗 Static utility methods traverse and sequence
  • 🎨 Platform agnostic - zero Flutter dependencies required

Usage #

Basic Usage #

// Create a success response
final success = TurboResponse.success(
  result: 'Hello',
  title: 'Greeting',
  message: 'Welcome message',
);

// Create a fail response
final fail = TurboResponse.fail(
  error: Exception('Something went wrong'),
  title: 'Error',
  message: 'Operation failed',
);

// Pattern match on the response
final message = response.when(
  success: (response) => 'Got result: ${response.result}',
  fail: (response) => 'Failed with: ${response.error}',
);

Async Operations #

// Transform a value asynchronously
final lengthResponse = await stringResponse.mapSuccess(
  (value) async => await computeLength(value),
);

// Chain async operations
final result = await response
  .andThen((value) async => await validateValue(value))
  .andThen((value) async => await saveValue(value));

Utility Methods #

// Get the value or throw
try {
  final value = response.unwrap();
  print('Got value: $value');
} catch (e) {
  print('Failed: $e');
}

// Get the value or a default
final value = response.unwrapOr('default');

// Validate a success value
final validated = response.ensure(
  (value) => value > 0,
  error: 'Value must be positive',
);

Static Utilities #

// Process a list of items
final result = await TurboResponseX.traverse(
  items,
  (item) => processItem(item),
);

// Combine multiple responses
final responses = [response1, response2, response3];
final combined = TurboResponseX.sequence(responses);

Installation #

Add this to your package's pubspec.yaml file:

dependencies:
  turbo_response: ^0.2.4

Works out of the box with both Dart and Flutter projects - no additional setup required!

Additional information #

For more examples and detailed API documentation, visit the API reference.

License #

This project is licensed under the MIT License - see the LICENSE file for details.

0
likes
150
points
109
downloads

Publisher

verified publisherultrawideturbodev.com

Weekly Downloads

A type-safe response wrapper for handling success and failure states with pattern matching and utility methods.

Homepage
Repository (GitHub)
View/report issues

Topics

#error-handling #result #response #type-safe #dart

Documentation

API reference

License

MIT (license)

More

Packages that depend on turbo_response