class LoginPageState extends State<LoginPage> {
String? name;
int? password;
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.fromLTRB(10.0, 0.0, 10.0, 0.0),
child: Center(
child: Container(
width: MediaQuery.of(context).size.width, //屏幕宽度
height: 200.0,
child: Column(
children: [
const Text('login', style: TextStyle(fontSize: 26.0)),
Form(
child: TextFormField(
validator: (value) {
print(value);
},
onChanged: (newValue) {
name = newValue;
print(name!.length);
},
decoration: const InputDecoration(
prefixIcon: Icon(Icons.person),
hintText: '请输入用户名',
suffixIcon: name.length > 0 ? Icon(Icons.cancel) : null),
)),
Form(
child: TextFormField(
validator: (value) {
print(value);
},
decoration: const InputDecoration(
prefixIcon: Icon(Icons.lock),
hintText: '请输入密码',
suffixIcon: Icon(Icons.remove_red_eye_sharp)),
)),
],
),
),
),
);
}
}
name.length > 0 ? Icon(Icons.cancel) : null) 这个报错可能是name不存在,如果name是null或者underfied,那肯定不能使用length啊,所以你得先判断name。
name&&name.length > 0 ? Icon(Icons.cancel) : null
你的null后面多了个反括号