【发布时间】:2021-04-07 13:14:16
【问题描述】:
所以最近我一直在做一个包含测验的小型移动应用项目。我希望我的应用程序做的是询问用户是否想退出测验或留下,但只有当我的问题索引为 0 时(因为我已经让我的用户可以回到问题)。所以我写下了问题布局小部件,并将 WillPopScope 小部件放在 Scaffold 上方,这样我就可以使用他的 onWillPop 属性了。
return BlocProvider(
create: (context) => CardsCubit(),
child: ScreenUtilInit(
designSize: Size(414, 896),
allowFontScaling: true,
builder: () => WillPopScope(
onWillPop: exitFromExamRequest(context) ,
child: Scaffold(
body: ...
还有我的 exitFromExamRequest 函数:
Future<bool> exitFromExamRequest(BuildContext context) async {
if (context.read<ExamQuestionIndexCubit>().state == 0) {
return await showDialog(
context: context,
builder: (context) => AlertDialog(
title: Text(
'Czy napewno chcesz opuścić test? Twoje odpowiedzi nie zostaną zapisane!',
style: kTitleTextStyle.copyWith(
fontSize: kSpacingUnit.w * 1.8,
),
),
actions: [
TextButton(
child: Text('Tak',
style: TextStyle(
color: Theme.of(context).textTheme.bodyText1!.color)),
onPressed: () => Navigator.pop(context, true),
),
TextButton(
child: Text('Nie',
style: TextStyle(
color: Theme.of(context).textTheme.bodyText1!.color)),
onPressed: () => Navigator.pop(context, false),
),
],
));
}
// return await Future.value(false); Doesn't work
// return await Future(() => false); Doesn't work either
}
我使用 await Future.value(false) 或 await Future(() => false) 的方式不起作用,并且出现错误提示:
The argument type 'Future<bool>' can't be assigned to the parameter type 'Future<bool> Function()?'.
这个问题有什么解决办法吗??我会很高兴为您提供一个方法:)
【问题讨论】: