data_handler 0.0.1
data_handler: ^0.0.1 copied to clipboard
Handle Data Manage State With GetX and State Management
DataHandler โจ๐๐ฏ #
DataHandler is a lightweight and efficient state management utility for handling API responses in Flutter applications. It simplifies managing different states like loading, success, error, and empty states, making UI updates seamless. ๐ฏ๐ฑ๐ฅ
Features ๐จโก๐ ๏ธ #
- Manage API response states easily.
- Built-in loading, success, error, and empty state handling.
- Provides flexible widget builders for UI rendering.
- Works with any data type (
T).
Installation ๐ฅ๐ง๐ #
Add the following dependency to your pubspec.yaml file:
dependencies:
data_handler: latest_version # Replace with the latest version
Then, run:
flutter pub get
Usage ๐๐ฅ๏ธ๐ฏ #
1. Import the Package ๐ฆโ ๐ #
import 'package:data_handler/data_handler.dart';
2. Initialize DataHandler ๐ฏ๐โก #
final handler = DataHandler<String>();
3. Manage API Responses ๐๐กโก #
Start Loading โณ๐๐
handler.startLoading();
On Success ๐โ ๐
handler.onSuccess("Data loaded successfully");
On Error โโ ๏ธ๐จ
handler.onError("Something went wrong");
On Empty Data ๐ญโก๐
handler.onEmpty("No data available");
4. Use when for UI Handling ๐ญ๐ฑ๐ #
Widget build(BuildContext context) {
return handler.when(
context: context,
loadingBuilder: (ctx) => CircularProgressIndicator(),
successBuilder: (data) => Text(data),
errorBuilder: (error) => Text("Error: $error"),
emptyBuilder: (message) => Text("Empty: $message"),
);
}
5. Use whenListWidget for Lists ๐๐๏ธโก #
List<Widget> buildList(BuildContext context) {
return handler.whenListWidget(
context: context,
loadingBuilder: (ctx) => Center(child: CircularProgressIndicator()),
successBuilder: (data) => [Text("Item: $data")],
errorBuilder: (error) => [Text("Error: $error")],
emptyBuilder: (message) => [Text("No items found")],
);
}
Contributing ๐ค๐ก๐ #
Contributions are welcome! Feel free to open issues or submit pull requests. ๐โ ๐ฏ
License ๐โ โ๏ธ #
This package is licensed under the MIT License. ๐๐โ