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.
- 🎯 Type-safe success and failure states
- 🔄 Pattern matching with
when
andmaybeWhen
- 🛠️ Transformation methods like
mapSuccess
,mapFail
, andandThen
- ⚡ Async operation support
- 🎁 Utility methods like
unwrap
,unwrapOr
, andensure
- 🔗 Static utility methods
traverse
andsequence
- 🎨 Platform agnostic - zero Flutter dependencies required
// 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}',
);
// 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));
// 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',
);
// 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);
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!
For more examples and detailed API documentation, visit the API reference.
This project is licensed under the MIT License - see the LICENSE file for details.