async_error_boundary 0.1.0 copy "async_error_boundary: ^0.1.0" to clipboard
async_error_boundary: ^0.1.0 copied to clipboard

Declarative error boundary widget for Flutter inspired by React's Error Boundary. Catch errors thrown by descendants and render fallback UI with reset support.

example/lib/main.dart

import 'package:async_error_boundary/async_error_boundary.dart';
import 'package:async_zone/async_zone.dart';
import 'package:flutter/material.dart';

void main() => runApp(const MyApp());

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      home: Scaffold(body: Center(child: HomePage())),
    );
  }
}

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  bool _shouldCrash = true;

  @override
  Widget build(BuildContext context) {
    return ErrorBoundary(
      onReset: (_) => setState(() => _shouldCrash = false),
      builder: (context, error, reset) => Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          Text('Caught: $error'),
          const SizedBox(height: 12),
          FilledButton(onPressed: reset, child: const Text('Reset')),
        ],
      ),
      child: _Crasher(shouldCrash: _shouldCrash),
    );
  }
}

class _Crasher extends ZoneWidget {
  const _Crasher({required this.shouldCrash});

  final bool shouldCrash;

  @override
  Widget build(BuildContext context) {
    if (shouldCrash) throw Exception('boom');
    return const Text('All good', style: TextStyle(fontSize: 20));
  }
}
0
likes
160
points
74
downloads

Documentation

API reference

Publisher

verified publisherkyoheig3.jp

Weekly Downloads

Declarative error boundary widget for Flutter inspired by React's Error Boundary. Catch errors thrown by descendants and render fallback UI with reset support.

Repository (GitHub)
View/report issues

Topics

#error-boundary #error-handling #react #declarative

License

BSD-3-Clause (license)

Dependencies

async_zone, flutter

More

Packages that depend on async_error_boundary