如何解决在空值上使用空检查运算符与空安全冲突
我有空安全问题。
我想在我的类中创建函数 onFieldSubmitted 和 onChanged,如下所示
import 'package:Flutter/material.dart';
import 'package:Flutter/services.dart';
import 'package:rift/constants.dart';
class DateTextField extends StatelessWidget {
final String dateComposition;
final String dateCompositionHintText;
final dynamic Function()? onFieldSubmitted;
//onchanged function has to be determined if you want to automatically set the focus to another text field,see application on age_screen with the datetextfields
final Function()? onChanged;
//set the widget with its focusnode
final FocusNode? focusNode;
并想将它们放入我的 textformfield
child: TextFormField(
onFieldSubmitted: onFieldSubmitted!(),onChanged: onChanged!(),focusNode: focusNode!,
问题是我收到错误“在空值上使用了空检查运算符”,并且由于 onFieldSubmitted 和 onChanged 函数的 bang (!)。此错误会导致应用出现渲染问题 [1]:https://i.stack.imgur.com/7ALjp.png [Rederflex][1]。
另一方面,如果我删除 bang (!),我会收到消息“无法无条件调用该函数,因为它可以为 'null'。尝试添加一个 null 检查 ('!')”>
基本上我现在处于循环中,不知道如何解决
我会很高兴得到一些帮助 最好 瑞德
完整代码:
import 'package:Flutter/material.dart';
import 'package:Flutter/services.dart';
import 'package:rift/constants.dart';
class DateTextField extends StatelessWidget {
final String dateComposition;
final String dateCompositionHintText;
final dynamic Function()? onFieldSubmitted;
//onchanged function has to be determined if you want to automatically set the focus to another text field,see application on age_screen with the datetextfields
final Function()? onChanged;
//set the widget with its focusnode
final FocusNode? focusNode;
DateTextField({required this.dateComposition,required this.dateCompositionHintText,this.onFieldSubmitted,this.onChanged,this.focusNode});
//dateComposition can only have Day,Month,or Year as strings
@override
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,crossAxisAlignment: CrossAxisAlignment.center,children: <Widget>[
Text(
dateComposition,style: TextStyle(fontSize: 18,fontWeight: FontWeight.w600),),SizedBox(height: 7),Container(
margin: EdgeInsets.all(4),width: dateComposition == "Year"? 73: 55,child: TextFormField(
onFieldSubmitted: onFieldSubmitted(),// onChanged: onChanged!(),textAlign: TextAlign.center,//Keyboardtype for numbers
keyboardType: TextInputType.number,//only numbers can be typed in
inputFormatters: <TextInputFormatter>[
FilteringTextInputFormatter.digitsOnly,LengthLimitingTextInputFormatter(dateComposition=="Year"? 4 : 2),],autofocus: true,cursorColor: kPrimarymagentaColor,decoration: Inputdecoration(
enabledBorder: OutlineInputBorder(
borderSide: BorderSide(color: kTextIconColorDarkBlue),borderRadius: BorderRadius.circular(15),hintText: dateCompositionHintText,hintStyle: TextStyle(fontWeight: FontWeight.w600,fontSize: 18.0),contentPadding: EdgeInsets.only(
left: 10,right: 10,top: 10,bottom: 10,focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(15),borderSide: BorderSide(
color: kPrimarymagentaColor,width: 1.5,);
}
}
错误日志
═══════ Exception caught by widgets library ═══════════════════════════════════
The following _CastError was thrown building DateTextField(dirty):
Null check operator used on a null value
The relevant error-causing widget was
DateTextField
lib/…/age_screen/age_screen.dart:46
When the exception was thrown,this was the stack
#0 DateTextField.build
package:rift/…/age_screen/date_text_field.dart:36
#1 StatelessElement.build
package:Flutter/…/widgets/framework.dart:4648
#2 ComponentElement.performRebuild
package:Flutter/…/widgets/framework.dart:4574
#3 Element.rebuild
package:Flutter/…/widgets/framework.dart:4267
#4 ComponentElement._firstBuild
package:Flutter/…/widgets/framework.dart:4553
...
════════════════════════════════════════════════════════════════════════════════
════════ Exception caught by widgets library ═══════════════════════════════════
Null check operator used on a null value
The relevant error-causing widget was
DateTextField
lib/…/age_screen/age_screen.dart:57
════════════════════════════════════════════════════════════════════════════════
════════ Exception caught by widgets library ═══════════════════════════════════
Null check operator used on a null value
The relevant error-causing widget was
DateTextField
lib/…/age_screen/age_screen.dart:68
════════════════════════════════════════════════════════════════════════════════
════════ Exception caught by rendering library ═════════════════════════════════
A RenderFlex overflowed by 299572 pixels on the right.
The relevant error-causing widget was
Row
lib/…/age_screen/age_screen.dart:41
The specific RenderFlex in question is: RenderFlex#70b39 relayoutBoundary=up2 OVERFLOWING
════════════════════════════════════════════════════════════════════════════════
════════ Exception caught by rendering library ═════════════════════════════════
A RenderFlex overflowed by 99498 pixels on the bottom.
The relevant error-causing widget was
Column
lib/…/age_screen/age_screen.dart:25
════════════════════════════════════════════════════════════════════════════════
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。