gpt4 book ai didi

Flutter:TextField onChanged 处理程序默默吞下异常

转载 作者:行者123 更新时间:2023-12-04 08:43:01 26 4
gpt4 key购买 nike

TextField.onChanged 中抛出异常且未处理时处理程序它不会冒泡到全局 Flutter.onError处理程序,所以它被默默地错过了。有没有办法全局处理这些错误,以便我至少知道它们在开发时被抛出?
它似乎被捕获并转换为 MethodChannel._handleAsMethodCall() 中的对象,但我不明白它是如何从那里处理的。

main() {
runApp(Test());
}

class Test extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Center(
child: TextField(
decoration: InputDecoration(
labelText: "Input",
),
onChanged: (input) {
throw Exception(); // <-------- swallowed by framework
},
),
),
),
);
}
}

最佳答案

问题是你必须调用WidgetsFlutterBinding.ensureInitialized()在您分配自定义错误处理程序之前 - 以下代码适用于我:

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
void main() {
WidgetsFlutterBinding.ensureInitialized(); // <- this needs to happen before assigning your error handler
FlutterError.onError = (FlutterErrorDetails details) => print('custom error handling');
runApp(Test());
}

class Test extends StatelessWidget {

@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Center(
child: TextField(
decoration: InputDecoration(
labelText: "Input",
),
onChanged: (input) {
print(input);
throw Exception();
},
),
),
),
);
}
}
写入 'uiae' 时的控制台输出:
Performing hot restart...
Syncing files to device Chrome...
Restarted application in 434ms.
u
custom error handling
ui
custom error handling
uia
custom error handling
uiae
custom error handling
您可能还想看看 Flutter: How to implement FlutterError.OnError correctlyFlutter catching all unhandled exceptions因为它们似乎与您的问题有关。

关于Flutter:TextField onChanged 处理程序默默吞下异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64470385/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com