async_lite 1.0.0
async_lite: ^1.0.0 copied to clipboard
Lightweight version of the popular package
async_lite #
Lightweight asynchronous utilities for Dart applications.
β¨ Features #
- Result Type - Type-safe error handling with
ValueResultandErrorResult - UI State - Convert
Resultto UI states (LoadingUiState,ValueUiState,ErrorUiState) - LazyFutureExt - Automatic caching for concurrent async calls
- AsyncCache - Flexible caching with customizable TTL
- Minimal Dependencies - Lightweight and performant
π Why async_lite? #
- Prevent Duplicate Calls - Multiple simultaneous calls to the same async function return the same result
- Type Safety - Explicit error handling without try-catch blocks
- Zero Boilerplate - Simple API that just works
- Production Ready - Battle-tested in real applications
π‘ Usage #
LazyFutureExt - Prevent Duplicate API Calls #
import 'package:async_lite/async_lite.dart';
// Multiple calls return the same result
for (var i = 0; i < 5; i++) {
fetchData.byLazy().then((result) => print(result));
}
// Only one actual call is made!
Future<String> fetchData() async {
await Future.delayed(Duration(seconds: 2));
return 'Data loaded';
}
Result Type - Clean Error Handling #
Future<Result<User>> getUser(String id) async {
try {
final user = await api.fetchUser(id);
return ValueResult(user);
} catch (e) {
return ErrorResult(e.toString());
}
}
// Usage
final result = await getUser('123');
switch (result) {
case ValueResult(:final value):
print('User: $value');
case ErrorResult(:final error):
print('Error: $error');
}
AsyncCache - Flexible Caching #
final cache = AsyncCache<Data>(Duration(minutes: 5));
// First call fetches data
final data1 = await cache.fetch(() => fetchExpensiveData());
// Subsequent calls return cached result
final data2 = await cache.fetch(() => fetchExpensiveData());
UI State - Simplify State Management #
import 'package:async_lite/src/ui_state.dart';
// Convert Result to UI State
final result = await fetchUser();
final state = result.toUiState();
// Handle different states
switch (state.type) {
case UiState.loading:
showLoader();
case UiState.value:
showData(state.valueOrNull);
case UiState.error:
showError(state.messageOrNull);
}
π Full Example #
See example/main.dart for complete usage examples.
π License #
MIT License - see LICENSE file for details.