gpt4 book ai didi

flutter - 如何解决意外的空值

转载 作者:行者123 更新时间:2023-12-04 17:15:13 25 4
gpt4 key购买 nike

我在我的应用程序中添加了一个 float 按钮,同时单击它出现两个文本字段,没有添加 formKey 该按钮有效但是当我使用它来控制字段文本是否为空时if 语句 if (formKey.currentState!.validate()) 在调试控制台中出现此错误

════════ Exception caught by gesture ═══════════════════════════════════════════
Unexpected null value.
═══════════════════════════════════════════════════════════════════════════════

这是 float 按钮的代码

 floatingActionButton: FloatingActionButton(
onPressed: () {
if (isBottomSheetShown) {
if (formKey.currentState!.validate()) {
Navigator.pop(context);
isBottomSheetShown = false;
setState(() {
fabIcon = Icons.edit;
});
}
} else {
scaffoldKey.currentState!.showBottomSheet(
(context) => Container(
color: Colors.grey[100],
padding: const EdgeInsets.all(20),
child: Form(
key: formKey,
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
TextField(
controller: titleController,
decoration: InputDecoration(
prefixIcon: Icon(Icons.title),
hintText: 'write a title',
labelText: 'Title',
border: OutlineInputBorder(),
),
),
SizedBox(
height: 15,
),
TextField(
controller: timeController,
decoration: InputDecoration(
prefixIcon: Icon(Icons.watch_later_outlined),
hintText: 'write a time',
labelText: 'Time',
border: OutlineInputBorder(),
),
onTap: () {
showTimePicker(
context: context,
initialTime: TimeOfDay.now(),
).then((value) {
timeController.text =
value!.format(context).toString();
print(value!.format(context));
});
},
),
],
),
),
),
);
}
isBottomSheetShown = true;
setState(() {
fabIcon = Icons.add;
});
},
child: Icon(
fabIcon,
)),

最佳答案

出现意外的 null 值是因为您在验证之前断言 formKey.currentState 不为 null,因此如果 currentState 等于 null,它将抛出该错误。

我建议在调用验证方法之前先检查是否 formKey.currentState != null

关于flutter - 如何解决意外的空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68846274/

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