gpt4 book ai didi

flutter - 如何使用_formKey验证TextFormField?

转载 作者:行者123 更新时间:2023-12-03 04:46:29 25 4
gpt4 key购买 nike

我说错了

The following NoSuchMethodError was thrown while handling a gesture: The method 'validate' was called on null



我相信我需要包装TextFormFields,但是不确定如何执行/编码。
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import '../../shared/constants.dart';

class PasswordReset extends StatefulWidget {
@override
_PasswordResetState createState() => _PasswordResetState();
}

class _PasswordResetState extends State<PasswordReset> {
final _formKey = GlobalKey<FormState>();
String email = '';
String error = '';

@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.purple[200],
appBar: AppBar(
backgroundColor: Colors.purple[800],
title: Text('Reset Password'),
),
body: Center(
key: _formKey,
child: Padding(
padding: EdgeInsets.symmetric(vertical: 20.0, horizontal: 50.0),
child: Column(
children: <Widget>[
TextFormField(
decoration:
textInputDecoration.copyWith(hintText: 'Email'),
validator: (value) {
if (value.isEmpty) {
return "Please enter your email";
} else {
email = value;
}
return null;
}),
Padding(
padding: EdgeInsets.symmetric(
vertical: 20.0, horizontal: 50.0),
child: RaisedButton(
color: Colors.blue[400],
child: Text('submit',style: TextStyle(color: Colors.white)),
onPressed: () {
if (_formKey.currentState.validate()) {
FirebaseAuth.instance
.sendPasswordResetEmail(email: email)
.then((value) => print("Check inbox"));
}
}))
],
))));
}
}

最佳答案

您可以在下面复制粘贴运行完整代码
您可以将ColumnForm一起使用,并将_formKey移至Form
程式码片段

Center(
child: Padding(
padding: EdgeInsets.symmetric(vertical: 20.0, horizontal: 50.0),
child: Form(
key: _formKey,
child: Column(

工作演示

enter image description here

完整的代码
import 'package:flutter/material.dart';

class PasswordReset extends StatefulWidget {
@override
_PasswordResetState createState() => _PasswordResetState();
}

class _PasswordResetState extends State<PasswordReset> {
final _formKey = GlobalKey<FormState>();
String email = '';
String error = '';

@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.purple[200],
appBar: AppBar(
backgroundColor: Colors.purple[800],
title: Text('Reset Password'),
),
body: Center(
child: Padding(
padding: EdgeInsets.symmetric(vertical: 20.0, horizontal: 50.0),
child: Form(
key: _formKey,
child: Column(
children: <Widget>[
TextFormField(
/*decoration:
textInputDecoration.copyWith(hintText: 'Email'),*/
validator: (value) {
if (value.isEmpty) {
return "Please enter your email";
} else {
email = value;
}
return null;
}),
Padding(
padding: EdgeInsets.symmetric(
vertical: 20.0, horizontal: 50.0),
child: RaisedButton(
color: Colors.blue[400],
child: Text('submit',
style: TextStyle(color: Colors.white)),
onPressed: () {
if (_formKey.currentState.validate()) {
/*FirebaseAuth.instance
.sendPasswordResetEmail(email: email)
.then((value) => print("Check inbox"));*/
}
}))
],
),
)),
));
}
}

void main() {
runApp(MyApp());
}

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: PasswordReset(),
);
}
}

关于flutter - 如何使用_formKey验证TextFormField?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62184986/

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