gpt4 book ai didi

android - Flutter Form:方法 'validate'在null上调用。 [所有配置均已配置]

转载 作者:行者123 更新时间:2023-12-03 04:54:34 27 4
gpt4 key购买 nike

请帮我!我正在创建简单的登录页面,但出现如下异常:

══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY╞════════════════ [+3s] I/flutter (11275): The following NoSuchMethodError was thrown building LoginScreen(dirty, dependencies: [MediaQuery]): [ ] I/flutter (11275): The method 'validate' was called on null. [
] I/flutter (11275): Receiver: null [ ] I/flutter (11275): Tried calling: validate() [ ] I/flutter (11275): [ ] I/flutter (11275): The relevant error-causing widget was: [ ] I/flutter (11275): LoginScreen



虽然我认为,我已经配置了所有需要配置。但是不知何故,我的代码抛出了异常。如何解决?

这是我的代码:
import 'package:flutter/material.dart';
import 'package:flutter_icons/flutter_icons.dart';
import 'package:my_app/services/UserService.dart';

/*
* This is login screen view.
*
* If you want to edit form section for example email and password fields, just edit these below functions.
* - _formEmailWidget()
* - _formPasswordWidget()
* If you want to edit social login section, just edit _socialLoginWidget() function.
* If you want to edit or change logo, just edit _logoWidget() function.
*/
class LoginScreen extends StatelessWidget {
static final Pattern _pattern =
r'^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$';
final GlobalKey<FormState> _key = GlobalKey<FormState>();
final TextEditingController _emailController = TextEditingController();
final TextEditingController _passwordController = TextEditingController();

final UserAuth _auth = UserAuth.instance();

final Status status;

LoginScreen({@required this.status});

@override
Widget build(BuildContext context) {
return Scaffold(
resizeToAvoidBottomPadding: false,
body: SafeArea(child: this._bodyWidgets(context)));
}

_logoWidget() {
return Padding(
padding: const EdgeInsets.only(top: 15.0, bottom: 50.0),
child: Image(image: AssetImage('assets/images/logo.png')));
}

_formEmailWidget() {
return Container(
decoration: BoxDecoration(
border:
Border(bottom: BorderSide(color: Colors.white, width: 1.0))),
child: TextFormField(
controller: _emailController,
keyboardType: TextInputType.emailAddress,
validator: (String value) {
RegExp regex = new RegExp(_pattern);
if (regex.hasMatch(value)) {
return null;
}
return "Please, Enter valid e-mail";
},
decoration: const InputDecoration(
icon: Icon(
FontAwesome.user,
color: Colors.white,
),
fillColor: Colors.white,
hintStyle: TextStyle(
color: Colors.white,
fontFamily: 'ProximaThin',
),
hintText: 'Your e-mail'),
));
}

_formPasswordWidget() {
return Container(
decoration: BoxDecoration(
border:
Border(bottom: BorderSide(color: Colors.white, width: 1.0))),
child: TextFormField(
controller: _passwordController,
validator: (String value) {
if (value.isEmpty || value.length < 6) {
return "Password must be more than 6 characters";
}
return null;
},
decoration: const InputDecoration(
icon: Icon(
FontAwesome.key,
color: Colors.white,
),
fillColor: Colors.white,
hintStyle: TextStyle(
color: Colors.white,
fontFamily: 'ProximaThin',
),
hintText: 'Your password'),
obscureText: true,
));
}

_submitRowWidget() {
return Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
InkWell(
onTap: null,
child: Text(
'forget password?',
style: TextStyle(color: Colors.white),
),
),
OutlineButton(
textColor: Colors.white,
highlightedBorderColor: Colors.white,
borderSide: BorderSide(
color: Colors.white, width: 0.8, style: BorderStyle.solid),
shape: OutlineInputBorder(borderRadius: BorderRadius.circular(10.0)),
onPressed: _loginAction(),
child: Text(
'Login',
style: TextStyle(color: Colors.white),
),
)
],
);
}

_loginAction() {
if (_key.currentState.validate()) {
_auth.signIn(_emailController.text, _passwordController.text);
} else {
AlertDialog(
content: Text(
"Your e-mail or password doesn't match, Would you like create an account?"),
elevation: 24.0,
shape: OutlineInputBorder(borderRadius: BorderRadius.circular(10)),
actions: <Widget>[
FlatButton(
onPressed: null,
child: Text("Yes"),
)
],
);
}
}

_socialLoginWidget() {
return Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Container(
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.all(Radius.circular(10)),
),
child: FlatButton(
padding: const EdgeInsets.only(
left: 10.0, top: 0.0, bottom: 0.0, right: 10.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Icon(FontAwesome.facebook_f, color: Colors.black),
SizedBox(
width: 10,
),
Text('Facebook',
style: TextStyle(
fontFamily: 'Proxima',
fontSize: 11,
color: Colors.black))
],
),
onPressed: null,
),
),
Container(
width: 100,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.all(Radius.circular(10)),
),
child: FlatButton(
padding: const EdgeInsets.only(
left: 10.0, top: 0.0, bottom: 0.0, right: 10.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Icon(
FontAwesome.google,
color: Colors.black,
),
SizedBox(
width: 10,
),
Text(
'Google',
style: TextStyle(
fontFamily: 'Proxima', fontSize: 11, color: Colors.black),
)
],
),
onPressed: null,
),
)
],
);
}

_bodyWidgets(BuildContext context) {
return Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
_logoWidget(),
Expanded(
child: Center(
child: Container(
width: MediaQuery.of(context).size.width - 20,
decoration: BoxDecoration(
color: Colors.black,
boxShadow: [
BoxShadow(
color: Color.fromRGBO(0, 0, 0, 0.35),
blurRadius: 15.0,
offset: Offset(0, -5))
],
borderRadius: BorderRadius.only(
topLeft: Radius.circular(30),
topRight: Radius.circular(30))),
child: Padding(
padding:
const EdgeInsets.only(top: 45.0, left: 45.0, right: 45.0),
child: Column(children: <Widget>[
Form(
key: this._key,
child: Column(
children: <Widget>[
_formEmailWidget(),
_formPasswordWidget(),
SizedBox(
height: 15.0,
),
_submitRowWidget()
],
),
),
SizedBox(
height: 45,
),
Text('or connect using',
style: TextStyle(
fontFamily: 'ProximaThin', color: Colors.white)),
SizedBox(
height: 15,
),
_socialLoginWidget(),
SizedBox(
height: 15,
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'Don\'t have an account?',
style: TextStyle(
fontFamily: 'ProximaThin', color: Colors.white),
),
SizedBox(
width: 5,
),
InkWell(
onTap: null,
child: Text('Sign up',
style: TextStyle(
fontFamily: 'Proxima', color: Colors.white)),
)
],
)
]),
),
),
))
]);
}
}

最佳答案

您在这里有两个选择:

  • 将波纹管方法OnPressed: _loginAction(),更改为OnPressed: _loginAction,
  • 或将同一行更改为OnPressed: () { _loginAction(); },

  • 固定代码如下:
      _submitRowWidget() {
    return Row(
    mainAxisAlignment: MainAxisAlignment.spaceBetween,
    children: <Widget>[
    InkWell(
    onTap: null,
    child: Text(
    'forget password?',
    style: TextStyle(color: Colors.white),
    ),
    ),
    OutlineButton(
    textColor: Colors.white,
    highlightedBorderColor: Colors.white,
    borderSide: BorderSide(
    color: Colors.white, width: 0.8, style: BorderStyle.solid),
    shape: OutlineInputBorder(borderRadius: BorderRadius.circular(10.0)),
    onPressed: _loginAction,
    child: Text(
    'Login',
    style: TextStyle(color: Colors.white),
    ),
    )
    ],
    );

    }

    关于android - Flutter Form:方法 'validate'在null上调用。 [所有配置均已配置],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60830702/

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