Flutter Validator String 需要 Null Aware 但编译器警告不需要
Flutter Validator String Requires Null Aware But Compiler Warns Not Needed
我有一个 TextFormField:
TextFormField(
textAlign:
TextAlign.center,
autovalidateMode:
AutovalidateMode
.onUserInteraction,
onChanged: (value) {},
controller:
firstNameTextInputController,
validator: (input) => //////////////////////////// HERE
(input!.length <
51 &&
input!.length >
1)
? null
: "Invalid First Name", ////// End
style: TextStyle(
color: HexColor
.fromHex(
"#000000"),
),
decoration:
InputDecoration(
border: InputBorder
.none,
filled: true,
hintStyle: Theme.of(
context)
.textTheme
.subtitle1!
.merge(TextStyle(
color: HexColor
.fromHex(
"#707070"))),
hintText:
"*Enter First Name",
fillColor: HexColor
.fromHex(
"#ffffff"),
focusedBorder:
OutlineInputBorder(
borderSide: BorderSide(
color: HexColor
.fromHex(
"#707070"),
width: 5),
borderRadius:
BorderRadius
.circular(
5),
),
),
))),
这会发出警告:
warning: The '!' will have no effect because the receiver can't be
null. (unnecessary_non_null_assertion at [athelite]
lib\Pages\PlayerEditPageDefaultState.dart:427)
所以我删除了感叹号,然后它变成了一个错误:
error: The property 'length' can't be unconditionally accessed because the receiver can be 'null'. (unchecked_use_of_nullable_value at [athelite] lib\Pages\PlayerEditPageDefaultState.dart:425)
编译器一点都不讨喜!使用 Flutter 2.0 执行此操作的正确方法是什么?
第一个警告列在this documentation
The analyzer produces this diagnostic when the operand of the !
operator can’t be null.
这是因为您在同一 &&
运算符的两侧使用运算符 !
:
...
(input!.length < 51 && input!.length > 1)
...
如果满足第一个条件,则第二个条件将对操作数 input
的非空值进行操作,从而产生上述警告。
要关闭它,只需删除右侧的 !
:
(input!.length < 51 && input.length > 1)
我有一个 TextFormField:
TextFormField(
textAlign:
TextAlign.center,
autovalidateMode:
AutovalidateMode
.onUserInteraction,
onChanged: (value) {},
controller:
firstNameTextInputController,
validator: (input) => //////////////////////////// HERE
(input!.length <
51 &&
input!.length >
1)
? null
: "Invalid First Name", ////// End
style: TextStyle(
color: HexColor
.fromHex(
"#000000"),
),
decoration:
InputDecoration(
border: InputBorder
.none,
filled: true,
hintStyle: Theme.of(
context)
.textTheme
.subtitle1!
.merge(TextStyle(
color: HexColor
.fromHex(
"#707070"))),
hintText:
"*Enter First Name",
fillColor: HexColor
.fromHex(
"#ffffff"),
focusedBorder:
OutlineInputBorder(
borderSide: BorderSide(
color: HexColor
.fromHex(
"#707070"),
width: 5),
borderRadius:
BorderRadius
.circular(
5),
),
),
))),
这会发出警告:
warning: The '!' will have no effect because the receiver can't be null. (unnecessary_non_null_assertion at [athelite] lib\Pages\PlayerEditPageDefaultState.dart:427)
所以我删除了感叹号,然后它变成了一个错误:
error: The property 'length' can't be unconditionally accessed because the receiver can be 'null'. (unchecked_use_of_nullable_value at [athelite] lib\Pages\PlayerEditPageDefaultState.dart:425)
编译器一点都不讨喜!使用 Flutter 2.0 执行此操作的正确方法是什么?
第一个警告列在this documentation
The analyzer produces this diagnostic when the operand of the
!
operator can’t be null.
这是因为您在同一 &&
运算符的两侧使用运算符 !
:
...
(input!.length < 51 && input!.length > 1)
...
如果满足第一个条件,则第二个条件将对操作数 input
的非空值进行操作,从而产生上述警告。
要关闭它,只需删除右侧的 !
:
(input!.length < 51 && input.length > 1)