smart_fetch 1.0.1
smart_fetch: ^1.0.1 copied to clipboard
Beautiful full-screen error pages for server side errors and no internet connection state.
SmartFetch #
A Flutter package that provides beautiful full-screen error pages and intelligent handling for server-side errors, no internet connection states, and timeouts. SmartFetch simplifies error handling in your Flutter applications with ready-to-use widgets and utilities.
Features #
β¨ Automatic Error Detection: Distinguishes between no internet, server errors, and timeouts π¨ Beautiful Error Screens: Pre-built, animated full-screen error pages using Lottie animations π Easy Integration: Simple API with both programmatic and widget-based approaches β‘ Smart Retry Logic: Built-in retry functionality for failed requests π― Type-Safe Results: Strongly-typed result objects for clean error handling π± Customizable UI: Override default error screens with your own designs π Connectivity Aware: Real-time internet connectivity checking
Getting Started #
Add smart_fetch to your pubspec.yaml:
dependencies:
smart_fetch: ^1.0.0
Then run:
flutter pub get
Usage #
Basic Example with SmartFetch.call() #
Use SmartFetch.call() for programmatic API calls with automatic error handling:
import 'package:smart_fetch/smart_fetch.dart';
import 'package:http/http.dart' as http;
Future<void> fetchData() async {
final result = await SmartFetch.call(
() => http.get(Uri.parse('https://api.example.com/data')),
timeout: const Duration(seconds: 10),
);
if (result.isSuccess) {
print('Success: ${result.response?.body}');
} else if (result.isNoInternet) {
print('No internet connection');
} else if (result.isTimeout) {
print('Request timed out');
} else {
print('Server error occurred');
}
}
Widget-Based Approach with SmartFetch.builder() #
Use SmartFetch.builder() in your Flutter widgets to automatically display appropriate error screens:
import 'package:flutter/material.dart';
import 'package:smart_fetch/smart_fetch.dart';
import 'package:http/http.dart' as http;
class MyWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('SmartFetch Example')),
body: SmartFetch.builder(
future: () => http.get(Uri.parse('https://api.example.com/data')),
onSuccess: (context, response) {
return Center(
child: Text('Data: ${response.body}'),
);
},
loadingWidget: Center(
child: CircularProgressIndicator(),
),
timeout: Duration(seconds: 10),
),
);
}
}
Custom Error Screens #
Override the default error screens with your own:
SmartFetch.builder(
future: () => http.get(Uri.parse('https://api.example.com/data')),
onSuccess: (context, response) {
return Text('Success: ${response.body}');
},
noInternetScreen: CustomNoInternetScreen(),
serverErrorScreen: CustomServerErrorScreen(),
timeoutScreen: CustomTimeoutScreen(),
);
Manual Connectivity Check #
Check internet connectivity manually:
import 'package:smart_fetch/smart_fetch.dart';
final hasInternet = await InternetChecker.hasInternetConnection();
if (hasInternet) {
// Proceed with API call
} else {
// Show offline UI
}
API Reference #
SmartFetch.call() #
Makes an HTTP request with automatic error handling.
Parameters:
request: A function that returns aFuture<http.Response>timeout: Optional timeout duration (default: 30 seconds)
Returns: SmartFetchResult with status and response data
SmartFetch.builder() #
A widget builder that automatically handles loading, success, and error states.
Parameters:
future: A function that returns aFuture<http.Response>onSuccess: Widget builder called when request succeedsloadingWidget: Optional custom loading widgetnoInternetScreen: Optional custom no internet screenserverErrorScreen: Optional custom server error screentimeoutScreen: Optional custom timeout screentimeout: Optional timeout duration
Example #
Check out the complete example in the /example folder:
cd example
flutter run
Contributing #
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
Issues #
If you encounter any issues or have suggestions, please file them in the issue tracker.
License #
This project is licensed under the MIT License - see the LICENCE file for details.
Author #
Nikesh Sejuwal
- GitHub: @nikesh-sejuwal
- Repository: smart_fetch
Acknowledgments #
- Uses Lottie for beautiful animations
- Uses connectivity_plus for network detection
- Built with β€οΈ for the Flutter community