data_handler 0.0.4 copy "data_handler: ^0.0.4" to clipboard
data_handler: ^0.0.4 copied to clipboard

Refactored architecture with enhanced global configuration for state management and data handling in Flutter applications. This package provides a robust solution for managing API states, including lo [...]

DataHandler πŸš€βœ¨πŸ“¦ #

Pub Version License Platform

Effortless State Management for API Responses in Flutter Apps! πŸŽ―πŸ“±βš‘

DataHandler is a lightweight and efficient state management utility designed to handle API responses seamlessly across all Flutter platforms (Android, iOS, Web, Windows, macOS, and Linux). It simplifies state handling, including loading, success, error, and empty states, ensuring a smooth UI experience with performance optimization and global configuration. πŸŽ‰πŸš€


🌟 Features #

βœ… Universal Compatibility – Works across all platforms!
βœ… Smart State Management – Loading, Success, Error, and Empty states with enum-based tracking
βœ… Performance Optimized – Only rebuilds when state actually changes
βœ… Global Widget Configuration – Set app-wide defaults for consistent UI
βœ… Sliver Support – Perfect integration with CustomScrollView
βœ… List Enhancement – Specialized handling for list data with empty detection
βœ… Works with Any Data Type (T) – Highly versatile and reusable
βœ… Modern Architecture – Built with latest Flutter best practices
βœ… Minimal Setup, Maximum Productivity – Get started in seconds!


πŸ“₯ Installation #

Add DataHandler to your pubspec.yaml:

dependencies:
  data_handler: ^0.0.4  # Use the latest version

Then, run:

flutter pub get

πŸš€ Quick Start #

1️⃣ Import the Package #

import 'package:data_handler/data_handler.dart';

2️⃣ Initialize DataHandler #

final DataHandler<String> handler = DataHandler<String>();
// Or with initial data
final DataHandler<List<User>> users = DataHandler<List<User>>(initialUserList);

3️⃣ Manage API States #

πŸ”„ Loading State

handler.startLoading();

βœ… Success State

handler.onSuccess("Data loaded successfully");

❌ Error State

handler.onError("Something went wrong");

πŸ“­ Empty State

handler.onEmpty("No data available");

πŸ”„ Convenient Refresh

await handler.refresh(() => apiService.fetchData());

πŸ–₯️ UI Integration #

🎭 Handle Different States Dynamically #

Widget build(BuildContext context) {
  return handler.when(
    onLoading: () => const CircularProgressIndicator(),
    onSuccess: (data) => Text(data),
    onError: (error) => Text("Error: $error", style: TextStyle(color: Colors.red)),
    onEmpty: (message) => Text("No Data: $message"),
  );
}

πŸ“‹ List Handling (Perfect for API Lists) #

Widget build(BuildContext context) {
  return userHandler.whenList(
    onSuccess: (users) => ListView.builder(
      itemCount: users.length,
      itemBuilder: (context, index) => UserTile(users[index]),
    ),
    onLoading: () => const Center(child: CircularProgressIndicator()),
    onError: (error) => ErrorWidget(error),
    onEmptyList: (message) => const EmptyUsersWidget(),
  );
}

🎒 Sliver Support for CustomScrollView #

Widget build(BuildContext context) {
  return CustomScrollView(
    slivers: [
      const SliverAppBar(title: Text('Users')),
      userHandler.whenSliverList(
        itemBuilder: (users, index) => UserTile(users[index]),
        itemCount: (users) => users.length,
        onLoading: () => const LoadingWidget(),
        onError: (error) => ErrorWidget(error),
        onEmpty: (message) => const EmptyWidget(),
      ),
    ],
  );
}

🌍 Global Configuration #

Set app-wide defaults for consistent UI across your entire application:

void main() {
  // Configure global widgets once
  DataHandlerConfig.setGlobalWidgets(
    loadingWidget: () => const CustomLoadingSpinner(),
    errorWidget: (error) => CustomErrorWidget(error),
    emptyWidget: (message) => CustomEmptyWidget(message),
  );
  
  runApp(MyApp());
}

new_break_changes_v_0.0.4 Now all your DataHandler instances will automatically use these widgets when local ones aren't provided! #

App Preview #

bSVbD.gif

Web Preview #

DataHandler Preview main


πŸš€ Advanced Usage #

πŸ” State Checking #

if (handler.isLoading) {
  // Show loading indicator
}

if (handler.hasSuccess) {
  // Data is available and valid
  print('Data: ${handler.data}');
}

if (handler.hasError) {
  // Handle error case
  print('Error: ${handler.errorMessage}');
}

πŸ“Š List-Specific Properties #

DataHandler<List<String>> listHandler = DataHandler();

// Check list state
print('Is empty: ${listHandler.isListEmpty}');
print('Length: ${listHandler.listLength}');

πŸ”„ Direct Data Updates #

// Update data without loading state (for real-time updates)
handler.updateData(newData);

// Clear all data and reset to empty
handler.clear();

🎯 Performance Optimization #

// Disable automatic state handling when needed
handler.when(
  enabled: false,  // Always shows success widget if data exists
  onSuccess: (data) => SuccessWidget(data),
  // ... other callbacks
);

// Control global widget fallback
handler.when(
  useGlobalWidgets: false,  // Only use local widgets
  onSuccess: (data) => SuccessWidget(data),
  // ... other callbacks
);

🌍 Cross-Platform Compatibility #

DataHandler is fully optimized for Flutter's multi-platform capabilities, ensuring smooth performance on:

Platform Status
πŸ“± Android βœ…
🍏 iOS βœ…
πŸ–₯️ Web βœ…
🏒 Windows βœ…
🍎 macOS βœ…
🐧 Linux βœ…

πŸ”„ Migration from v0.0.3 #

If you're upgrading from v0.0.3, here's what changed:

// OLD (v0.0.3)
handler.when(
  context: context,
  loadingBuilder: (context) => Loading(),
  successBuilder: (data) => Success(data),
  errorWidget: ErrorWidget(),
)

// NEW (v0.0.4)
handler.when(
  onLoading: () => Loading(),
  onSuccess: (data) => Success(data),
  onError: (error) => ErrorWidget(error),
)

Key Changes:

  • Removed context parameter
  • Changed builder names (loadingBuilder β†’ onLoading)
  • Replaced widget properties with callbacks
  • Added global configuration system

🀝 Contributing #

We love contributions! πŸš€

Feel free to open issues, discuss features, or submit pull requests to enhance DataHandler. Let's build something amazing together! πŸ› οΈβœ¨


πŸ“œ License #

This package is released under the MIT License. πŸ”“


Enjoying DataHandler? Give it a ⭐ on GitHub and help others discover it! πŸ˜ŠπŸš€

Made with ❀️ for the Flutter community

2
likes
150
points
32
downloads

Publisher

unverified uploader

Weekly Downloads

Refactored architecture with enhanced global configuration for state management and data handling in Flutter applications. This package provides a robust solution for managing API states, including loading, error handling, and data retrieval, while ensuring a clean and maintainable codebase.

Repository (GitHub)
View/report issues

Topics

#data #widget #data-handler #api #state

Documentation

API reference

License

MIT (license)

Dependencies

flutter

More

Packages that depend on data_handler