根据闭包上下文的要求,返回类型“future<bool>”不是“Future<bool>”
Posted
技术标签:
【中文标题】根据闭包上下文的要求,返回类型“future<bool>”不是“Future<bool>”【英文标题】:the return type 'future<bool>' isn't a 'Future<bool>', as required by the closure context 【发布时间】:2021-10-14 06:28:41 【问题描述】:我正在构建我的测验应用程序,我的测验页面正在成形,但我遇到了一个问题。我添加了一个 Willpopscope 只是为了让我的用户在测验开始时不能回到开头,它会弹出一个对话框。代码如下:
import 'dart:convert';
//import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
class GetJson extends StatelessWidget
const GetJson(Key? key) : super(key: key);
@override
Widget build(BuildContext context)
return FutureBuilder(
future: DefaultAssetBundle.of(context).loadString("assets/python.json"),
builder: (context, snapshot)
var mydata = jsonDecode(snapshot.data.toString());
if (mydata == null)
return Scaffold(
body: Center(
child: Text(
"Loading",
),
),
);
else
return QuizPage();
,
);
class QuizPage extends StatefulWidget
QuizPage(Key? key) : super(key: key);
@override
_QuizPageState createState() => _QuizPageState();
class _QuizPageState extends State<QuizPage>
Widget choicebutton()
return Padding(
padding: EdgeInsets.symmetric(
vertical: 10.0,
horizontal: 20.0,
),
child: MaterialButton(
onPressed: () ,
child: Text(
"option 1",
style: TextStyle(
color: Colors.white,
fontFamily: "Alike",
fontSize: 16.0,
),
maxLines: 1,
),
color: Colors.indigo,
splashColor: Colors.indigo[700],
highlightColor: Colors.indigo[700],
minWidth: 200.0,
height: 45.0,
shape:
RoundedRectangleBorder(borderRadius: BorderRadius.circular(20.0)),
),
);
@override
Widget build(BuildContext context)
SystemChrome.setPreferredOrientations(
[DeviceOrientation.portraitDown, DeviceOrientation.portraitUp]);
return WillPopScope(
onWillPop: ()
return showDialog(
context: context,
builder: (context) => AlertDialog(
title: Text(
"Quizstar",
),
content: Text("You Can't Go Back At This Stage"),
actions: [
// ignore: deprecated_member_use
FlatButton(onPressed: ()
Navigator.of(context).pop();
,
child: Text(
"Ok",
),
)
],
)
);
,
child: Scaffold(
body: Column(
children: [
Expanded(
flex: 3,
child: Container(
padding: EdgeInsets.all(15.0),
alignment: Alignment.bottomLeft,
child: Text(
"This is a sample question which will be displayed?",
style: TextStyle(
fontSize: 16.0,
fontFamily: "Quando",
),
),
),
),
Expanded(
flex: 6,
child: Container(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
choicebutton(),
choicebutton(),
choicebutton(),
choicebutton(),
],
),
),
),
Expanded(
flex: 1,
child: Container(
alignment: Alignment.topCenter,
child: Center(
child: Text(
"30",
style: TextStyle(
fontSize: 35.0,
fontWeight: FontWeight.w700,
fontFamily: "Times New Roman",
),
),
),
),
),
],
),
),
);
错误出现在 showDialog() 中,但我似乎无法弄清楚。
非常感谢各位。
【问题讨论】:
【参考方案1】:这可能会有所帮助
在您的 willpop
范围函数上改为这样做
onWillPop: ()
showDialog(
context: context,
builder: (context) => AlertDialog(
title: Text(
"Quizstar",
),
content: Text("You Can't Go Back At This Stage"),
actions: [
// ignore: deprecated_member_use
FlatButton(onPressed: ()
Navigator.of(context).pop();
,
child: Text(
"Ok",
),
)
],
)
);
return true;
),
child : your widget
【讨论】:
【参考方案2】:好吧,我想通了
return WillPopScope(
onWillPop: () async
print("Back Button Pressed");
final shouldPop = await showWarning(context);
return shouldPop ?? false;
,
然后我继续像这样定义 showWarning,然后在其中定义我的对话框:
Future<bool?> showWarning(BuildContext context) async => showDialog<bool>(
context: context,
builder: (context) => AlertDialog(
title: Text("Quizstar"),
content: Text("You cannot go back at this stage"),
actions: [
ElevatedButton(
onPressed: () => Navigator.pop(context, false),
child: Text("Okay"),
),
//ElevatedButton(
// onPressed: () => Navigator.pop(context, true),
// child: Text("Yes"),
//),
],
),
);
提示:不要忘记通过在其中写入“?”来将未来设为布尔值
【讨论】:
以上是关于根据闭包上下文的要求,返回类型“future<bool>”不是“Future<bool>”的主要内容,如果未能解决你的问题,请参考以下文章