excalibur 0.1.2+2 copy "excalibur: ^0.1.2+2" to clipboard
excalibur: ^0.1.2+2 copied to clipboard

A Flutter package for handling exceptions in a clean and simple way.

example/lib/main.dart

import 'package:excalibur/excalibur.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 MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class OddNumberException implements BladeException {
  @override
  int get code => 0;

  @override
  String get message => 'Odd Number Exception';

  @override
  bool get replace => false;
}

class EvenNumberException implements BladeException {
  @override
  int get code => 1;

  @override
  String get message => 'Even Number Exception';

  @override
  bool get replace => false;
}

class UnknownNumberException implements BladeException {
  @override
  int get code => 2;

  @override
  String get message => 'Unknown Number Exception';

  @override
  bool get replace => false;
}

class ErrorPage extends StatelessWidget {
  final BladeException exception;

  const ErrorPage({Key? key, required this.exception}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(exception.code.toString()),
      ),
      body: Center(
        child: Text(exception.message),
      ),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});
  final String title;
  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;
  late Excalibur excalibur;
  final _exceptions = [
    OddNumberException(),
    EvenNumberException(),
  ];

  void _incrementCounter() {
    setState(() {
      _counter++;
      try {
        if (_counter % 2 == 0) {
          throw EvenNumberException();
        } else {
          throw OddNumberException();
        }
      } catch (e) {
        excalibur.handleException(e as BladeException);
      }
    });
  }

  @override
  void initState() {
    excalibur = Excalibur(
      context,
      exceptions: _exceptions,
      unknownException: UnknownNumberException(),
      builder: (exception) => ErrorPage(exception: exception),
    );
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            const Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.headline4,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: const Icon(Icons.add),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}
2
likes
155
points
1
downloads

Publisher

unverified uploader

Weekly Downloads

A Flutter package for handling exceptions in a clean and simple way.

Repository (GitHub)

Documentation

API reference

License

MIT (license)

Dependencies

flutter

More

Packages that depend on excalibur