advanced_error_handler 1.0.2 copy "advanced_error_handler: ^1.0.2" to clipboard
advanced_error_handler: ^1.0.2 copied to clipboard

A Flutter package to handle errors with dialogs, toasts, and snack bars.

Advanced Error Handler for Flutter #

A simple and flexible advanced error handler package for Flutter apps that helps manage network and API errors. This package provides easy-to-use methods to display error messages in different formats (e.g., SnackBar, Toast, Dialogs) with retry options for network errors.

Features #

  • Handles network errors (e.g., no internet, timeout).
  • Handles API errors (e.g., HTTP 4xx/5xx errors).
  • Displays error messages using:
    • SnackBar (for API errors).
    • Toast (for quick feedback on API errors).
    • Dialog (for network errors with optional retry action).
  • Allows an optional retryCallback to retry failed network requests.

Getting started #

Add advanced_error_handler as a dependency in your pubspec.yaml:

dependencies:
  advanced_error_handler: ^1.0.2

Usage #

It includes code examples for handling both network and API errors, as well as how to use the optional retry callback. Add longer examples to /example folder.

import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:advanced_error_handler/advanced_error_handler.dart'; // Import the advanced error handler package

class ApiService {
  Future<void> fetchData(BuildContext context) async {
    final url = Uri.parse('https://api.example.com/data');

    try {
      final response = await http.get(url);

      if (response.statusCode >= 200 && response.statusCode < 300) {
        print('API Response: ${response.body}');
      } else {
        AdvancedErrorHandler.handleError(
          context,
          response, // API error
          retryCallback: () => fetchData(context),
        );
      }
    } catch (e) {
      AdvancedErrorHandler.handleError(
        context,
        e, // Network error
        retryCallback: () => fetchData(context),
      );
    }
  }
}

class MyApp extends StatelessWidget {
  final ApiService apiService = ApiService();

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: Text('Advanced Error Handler Example')),
        body: Center(
          child: ElevatedButton(
            onPressed: () {
              apiService.fetchData(context);
            },
            child: Text('Fetch Data'),
          ),
        ),
      ),
    );
  }
}

void main() {
  runApp(MyApp());
}

Optional Retry Callback The retry callback is optional. If you don't want to provide a retry action, simply omit it:

AdvancedErrorHandler.handleError(
  context,
  error, // The error object (either response or exception)
)

Additional information #

  • More Information:
    You can find more information about this package and its usage in the documentation on pub.dev, including API references and examples. Please refer to the example section for practical use cases.

  • Contributing:
    Contributions are welcome! If you'd like to improve the package, fix bugs, or add new features, please fork the repository and submit a pull request.

  • Filing Issues:
    If you find a bug or have a feature request, please open an issue in the GitHub issues section. When filing an issue, provide:

    • A detailed description of the issue or feature request.
    • Steps to reproduce the issue (if applicable).
    • Code snippets or error logs to help us diagnose the problem.

    We try to respond to issues within 2-3 business days, depending on the complexity.

  • Package Author Support:
    The maintainers of this package will address issues and provide assistance as needed. However, please understand that support may not be immediate. For faster responses, be sure to include all necessary details in your issue reports or questions.

  • License:
    This package is open-source and licensed under the MIT License. You are free to use, modify, and distribute this package under the terms of the license.

1
likes
120
points
31
downloads

Publisher

unverified uploader

Weekly Downloads

A Flutter package to handle errors with dialogs, toasts, and snack bars.

Homepage

Documentation

API reference

License

MIT (license)

Dependencies

flutter, fluttertoast, http

More

Packages that depend on advanced_error_handler