smart_fetch 1.0.1 copy "smart_fetch: ^1.0.1" to clipboard
smart_fetch: ^1.0.1 copied to clipboard

Beautiful full-screen error pages for server side errors and no internet connection state.

SmartFetch #

pub package License: MIT

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 a Future<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 a Future<http.Response>
  • onSuccess: Widget builder called when request succeeds
  • loadingWidget: Optional custom loading widget
  • noInternetScreen: Optional custom no internet screen
  • serverErrorScreen: Optional custom server error screen
  • timeoutScreen: Optional custom timeout screen
  • timeout: 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.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. 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

Acknowledgments #

  • Uses Lottie for beautiful animations
  • Uses connectivity_plus for network detection
  • Built with ❀️ for the Flutter community
3
likes
0
points
481
downloads

Publisher

unverified uploader

Weekly Downloads

Beautiful full-screen error pages for server side errors and no internet connection state.

Repository (GitHub)
View/report issues

Topics

#flutter #error-handling #timeout #no-internet #server-error

License

unknown (license)

Dependencies

connectivity_plus, flutter, http, lottie

More

Packages that depend on smart_fetch