按下后退按钮时如何处理 WillPopScope 小部件的空错误?
How to handle null error of WillPopScope widget when press back button?
我将 WillPopScope 小部件用于主页屏幕以防止应用程序
退出。当用户在没有确认提示对话框的情况下按下后退按钮时
body: WillPopScope(
onWillPop: () => StaticUI()
.onScreenPop(Routes.customerProcessScreen, context) as Future<bool>,
child: SingleChildScrollView(
...
),
);
Future<bool>? onScreenPop(String route, BuildContext context) async => await showDialog(
useSafeArea: true,
context: context,
barrierDismissible: true, // user must tap button!
builder: (BuildContext context) {
return Stack(
children: [
...
],
);
},
);
但是当我按下后退按钮关闭警报时(关闭警报时),我得到了一个空错误!
E/flutter ( 4688): [ERROR:flutter/lib/ui/ui_dart_state.cc(186)] Unhandled Exception: type 'Null' is not a subtype of type 'FutureOr<bool>'
E/flutter ( 4688): #0 StaticUI.onScreenPop (package:hesabate/Utils/statics.dart:275:74)
E/flutter ( 4688): <asynchronous suspension>
E/flutter ( 4688): #1 ModalRoute.willPop (package:flutter/src/widgets/routes.dart:1357:11)
E/flutter ( 4688): <asynchronous suspension>
E/flutter ( 4688): #2 NavigatorState.maybePop (package:flutter/src/widgets/navigator.dart:4966:45)
E/flutter ( 4688): <asynchronous suspension>
E/flutter ( 4688):
SDK 版本
environment:
sdk: '>=2.12.0 <3.0.0'
您需要 return true
或 false
来自 onScreenPop
函数。为此,请确保您 return 来自 showDialog
的 true
或 false
。请参阅 this link 以了解有关如何 return 来自 showDialog
的值的更多信息。
我在我的代码中做了一些编辑以检查 null returns,所以它现在可以工作了。
Future<bool> onScreenPop(String route, BuildContext context) async {
final result = await showDialog(
useSafeArea: true,
context: context,
barrierDismissible: true, // user must tap button!
builder: (BuildContext context) {
return Stack(
children: [
...
],
);
},
);
if (result == null) {
return false;
} else {
return result;
}
}
我将 WillPopScope 小部件用于主页屏幕以防止应用程序 退出。当用户在没有确认提示对话框的情况下按下后退按钮时
body: WillPopScope(
onWillPop: () => StaticUI()
.onScreenPop(Routes.customerProcessScreen, context) as Future<bool>,
child: SingleChildScrollView(
...
),
);
Future<bool>? onScreenPop(String route, BuildContext context) async => await showDialog(
useSafeArea: true,
context: context,
barrierDismissible: true, // user must tap button!
builder: (BuildContext context) {
return Stack(
children: [
...
],
);
},
);
但是当我按下后退按钮关闭警报时(关闭警报时),我得到了一个空错误!
E/flutter ( 4688): [ERROR:flutter/lib/ui/ui_dart_state.cc(186)] Unhandled Exception: type 'Null' is not a subtype of type 'FutureOr<bool>'
E/flutter ( 4688): #0 StaticUI.onScreenPop (package:hesabate/Utils/statics.dart:275:74)
E/flutter ( 4688): <asynchronous suspension>
E/flutter ( 4688): #1 ModalRoute.willPop (package:flutter/src/widgets/routes.dart:1357:11)
E/flutter ( 4688): <asynchronous suspension>
E/flutter ( 4688): #2 NavigatorState.maybePop (package:flutter/src/widgets/navigator.dart:4966:45)
E/flutter ( 4688): <asynchronous suspension>
E/flutter ( 4688):
SDK 版本
environment:
sdk: '>=2.12.0 <3.0.0'
您需要 return true
或 false
来自 onScreenPop
函数。为此,请确保您 return 来自 showDialog
的 true
或 false
。请参阅 this link 以了解有关如何 return 来自 showDialog
的值的更多信息。
我在我的代码中做了一些编辑以检查 null returns,所以它现在可以工作了。
Future<bool> onScreenPop(String route, BuildContext context) async {
final result = await showDialog(
useSafeArea: true,
context: context,
barrierDismissible: true, // user must tap button!
builder: (BuildContext context) {
return Stack(
children: [
...
],
);
},
);
if (result == null) {
return false;
} else {
return result;
}
}