我是flutter的新手,在这个简单的代码中我遇到了一个错误
用于空值的 NULL 检查运算符
我尝试了这里存在的所有解决方案(我在终端中运行所有命令,如颤振升级等),但我仍然遇到同样的错误。
import 'package:flutter/material.dart';
Future<void> main() async{
WidgetsFlutterBinding.ensureInitialized();
runApp(MaterialApp(
home: Home(),
));
}
class Home extends StatefulWidget {
const Home({Key? key}) : super(key: key);
@override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> {
final _key =GlobalKey<FormState>();
final textformfield=TextEditingController();
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Container(
child: Column(
children: [
SizedBox(height: 300,),
TextFormField(
controller: textformfield,
key: _key,
validator: (value){
if(value!.isEmpty){
return "Please enter your email";
}
else return null;
},
),
FlatButton(
onPressed: (){
if(_key.currentState!.validate()){
print("validate");
}
else print('NUll');
},
child: Text('Validate')
),
],
),
),
),
);
}
}