Flutter错误处理插件hondooye_error_handler的使用
hondooye_error_handler
特性
开始使用
使用
额外信息
```dart
import 'package:flutter/material.dart';
import 'package:hondooye_error_handler/hondooye_error_handler.dart';
import 'custom_error.dart';
import 'custom_exception.dart';
import 'theme_notifier.dart';
void main() {
runApp(ThemeNotifier(child: const MyApp()));
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
[@override](/user/override)
Widget build(BuildContext context) {
return ValueListenableBuilder(
valueListenable: ThemeNotifier.of(context)!.theme,
builder: (BuildContext context, ThemeData themeData, child) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Hondooye Package Test App',
theme: themeData,
home: const MyHomePage(title: 'Hondooye'),
);
},
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
[@override](/user/override)
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
ElevatedButton(
onPressed: () {
try {
// 创建一个冲突错误对象
HdyError hdyError = HdyErrors.conflict();
// 抛出错误
throw HdyException(hdyError);
} on HdyException {
// 捕获并显示错误
HdyErrorMessageHandler.showError(
context: context, widgetType: WidgetType.dialog);
}
},
child: const Text("Hondooye 异常"),
),
const SizedBox(
height: 24,
),
ElevatedButton(
onPressed: () {
try {
// 创建一个自定义错误对象
HdyError hdyError = CustomError("自定义错误");
// 抛出错误
throw CustomException(hdyError);
} on CustomException {
// 捕获并显示错误
HdyErrorMessageHandler.showError(
context: context, widgetType: WidgetType.dialog);
}
},
child: const Text("自定义异常"),
),
const SizedBox(
height: 24,
),
ElevatedButton(
onPressed: () {
try {
// 创建一个自定义错误对象
HdyError hdyError = CustomError("自定义错误消息对话框");
// 抛出错误
throw CustomException(hdyError);
} on CustomException {
// 捕获并显示错误
HdyErrorMessageHandler.showError(
context: context,
widgetType: WidgetType.dialog,
customErrorDialog: AlertDialog(
title: const Text('标题'),
content: const Text(
'消息',
),
actions: [
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
OutlinedButton(
onPressed: () {
// 清除错误消息
HdyErrorMessageHandler.flushErrorMessage();
// 关闭对话框
Navigator.of(context).pop();
},
child: const Text("按钮 1"),
),
const SizedBox(width: 24),
OutlinedButton(
onPressed: () {
// 清除错误消息
HdyErrorMessageHandler.flushErrorMessage();
// 关闭对话框
Navigator.of(context).pop();
},
child: const Text("按钮 2"),
),
],
),
],
shape: const RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(10))),
),
);
}
},
child: const Text("自定义错误对话框"),
),
],
),
),
);
}
}
更多关于Flutter错误处理插件hondooye_error_handler的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
1 回复