Lav*_*rma 3 dart flutter dart-null-safety
>2.12所以我正在进行更改,但我陷入了这个特定的错误,无法找到任何解决方案。non-nullable“空安全”问题后引起的 Future<bool?> _onBackPressed() async {
return showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text('Do you want to exit without saving changes?'),
content:
Text('Please press the SAVE button at the bottom of the page'),
actions: <Widget>[
TextButton(
child: Text('NO'),
onPressed: () {
Navigator.of(context).pop(false);
},
),
TextButton(
child: Text('YES'),
onPressed: () {
Navigator.of(context).pop(true);
Navigator.of(context).pop(true);
},
),
],
);
});
}
..
return WillPopScope(
onWillPop: () => _onBackPressed, // causing error
child: Scaffold(
key: _scaffoldkey,
body: SafeArea(
child: SingleChildScrollView(
child: Container(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Stack(
children: <Widget>[
Positioned(
child: AppBar(
centerTitle: true,
title: Text(
"Sample",
style: TextStyle(
fontFamily: 'LobsterTwo',
fontStyle: FontStyle.italic,
fontWeight: FontWeight.bold,
fontSize: 18.0,
color: Colors.black87,
),
),
backgroundColor: Colors.transparent,
elevation: 0,
leading: IconButton(
icon: Icon(
Icons.arrow_back,
color: Colors.black,
size: 30,
),
onPressed: () {
_onBackPressed();
},
..
..
),
),
),
],
),
],
),
),
),
),
),
);
Run Code Online (Sandbox Code Playgroud)
Sam*_*han 12
你可以这样尝试
onWillPop: () async {
bool? result= await _onBackPressed();
if(result == null){
result = false;
}
return result!;
},
Run Code Online (Sandbox Code Playgroud)
由于您的_onBackPressed函数返回Future<bool?>,并且onWillPop需要一个Future<bool>. 我所做的就是用来result获取您的回报_onBackPressed。返回值可能是true falseor null。所以我添加了一个 null 检查 null 值并将其更改为false。最后一件事是关键,使用!将 nullable 更改bool?为bool。result!意味着您保证它result不为空,即使您将其定义为可为空。
| 归档时间: |
|
| 查看次数: |
14347 次 |
| 最近记录: |