我正在创建Flutter应用程序,导航到此特定屏幕时出现问题。模拟器崩溃并显示一条消息:
NoSuchMethodError:方法“ validate”在null上被调用 接收者:null 尝试调用:validate()
我不明白为什么调用validate()方法,即使它包含在_submit函数中,该函数仅在按下屏幕上的按钮时才被调用。当我从onPressed中删除函数调用时:该应用程序可以正常工作。
import 'package:flutter/material.dart';
import 'package:service_request/services/auth_services.dart';
import 'package:flutter/services.dart';
class SignupForm extends StatefulWidget {
static const String id = 'signup_screen';
@override
_SignupFormState createState() => _SignupFormState();
}
class _SignupFormState extends State<SignupForm> {
final _signUpFormKey = GlobalKey<FormState>();
String _name, _email, _password;
_submit() {
if (_signUpFormKey.currentState.validate()) {
_signUpFormKey.currentState.save();
// Logging in the user w/ Firebase
AuthService.signUpUser(context, _name, _email, _password);
}
}
@override
Widget build(BuildContext context) {
return SafeArea(
child: Scaffold(
appBar: AppBar(
title: Text('Sign Up'),
),
body: SingleChildScrollView(
child: Form(
key: _signUpFormKey,
child: Padding(
padding: EdgeInsets.all(20.0),
child: Column(
children: <Widget>[
Padding(
padding: EdgeInsets.symmetric(vertical: 10.0),
child: TextFormField(
decoration: InputDecoration(
border: OutlineInputBorder(
borderSide: BorderSide(
color: Colors.black54,
),
),
labelText: 'Name',
),
validator: (input) => input.trim().isEmpty
? 'Please enter a valid name'
: null,
onSaved: (input) => _name = input,
),
),
Padding(
padding: EdgeInsets.symmetric(vertical: 10.0),
child: TextFormField(
keyboardType: TextInputType.emailAddress,
validator: (input) => !input.contains('@')
? 'Please enter a valid email'
: null,
onSaved: (input) => _email = input,
decoration: InputDecoration(
border: OutlineInputBorder(
borderSide: BorderSide(
color: Colors.black54,
),
),
labelText: 'Email Address',
),
),
),
Padding(
padding: EdgeInsets.symmetric(vertical: 10.0),
child: TextFormField(
validator: (input) => input.length < 6
? 'Must be at least 6 characters'
: null,
onSaved: (input) => _password = input,
obscureText: true,
decoration: InputDecoration(
border: OutlineInputBorder(
borderSide: BorderSide(
color: Colors.black54,
),
),
labelText: 'Password',
),
),
),
Column(
children: <Widget>[
Padding(
padding: EdgeInsets.symmetric(vertical: 10.0),
child: Container(
decoration: BoxDecoration(
color: Colors.blue,
borderRadius: BorderRadius.circular(10.0),
),
width: 350.0,
child: FlatButton(
onPressed: null,
child: Text(
'Sign Up',
style: TextStyle(
color: Colors.white,
fontSize: 18.0,
),
),
),
),
),
Padding(
padding: EdgeInsets.symmetric(vertical: 10.0),
child: Container(
decoration: BoxDecoration(
color: Colors.red,
borderRadius: BorderRadius.circular(10.0),
),
width: 350.0,
child: FlatButton(
onPressed: _submit(),
child: Text(
'Back to Log In',
style: TextStyle(
color: Colors.white,
fontSize: 18.0,
),
),
),
),
),
],
),
],
),
),
),
),
),
);
}
}
答案 0 :(得分:0)
_submit()
表示执行此功能。您必须删除()
您可以在
代码段
FlatButton(
onPressed: _submit,
child: Text(
'Back to Log In',
style: TextStyle(
color: Colors.white,
fontSize: 18.0,
),
),
)
工作演示
完整代码
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter/services.dart';
class SignupForm extends StatefulWidget {
static const String id = 'signup_screen';
@override
_SignupFormState createState() => _SignupFormState();
}
class _SignupFormState extends State<SignupForm> {
final _signUpFormKey = GlobalKey<FormState>();
String _name, _email, _password;
_submit() {
if (_signUpFormKey.currentState.validate()) {
_signUpFormKey.currentState.save();
// Logging in the user w/ Firebase
//AuthService.signUpUser(context, _name, _email, _password);
}
}
@override
Widget build(BuildContext context) {
return SafeArea(
child: Scaffold(
appBar: AppBar(
title: Text('Sign Up'),
),
body: SingleChildScrollView(
child: Form(
key: _signUpFormKey,
child: Padding(
padding: EdgeInsets.all(20.0),
child: Column(
children: <Widget>[
Padding(
padding: EdgeInsets.symmetric(vertical: 10.0),
child: TextFormField(
decoration: InputDecoration(
border: OutlineInputBorder(
borderSide: BorderSide(
color: Colors.black54,
),
),
labelText: 'Name',
),
validator: (input) => input.trim().isEmpty
? 'Please enter a valid name'
: null,
onSaved: (input) => _name = input,
),
),
Padding(
padding: EdgeInsets.symmetric(vertical: 10.0),
child: TextFormField(
keyboardType: TextInputType.emailAddress,
validator: (input) => !input.contains('@')
? 'Please enter a valid email'
: null,
onSaved: (input) => _email = input,
decoration: InputDecoration(
border: OutlineInputBorder(
borderSide: BorderSide(
color: Colors.black54,
),
),
labelText: 'Email Address',
),
),
),
Padding(
padding: EdgeInsets.symmetric(vertical: 10.0),
child: TextFormField(
validator: (input) => input.length < 6
? 'Must be at least 6 characters'
: null,
onSaved: (input) => _password = input,
obscureText: true,
decoration: InputDecoration(
border: OutlineInputBorder(
borderSide: BorderSide(
color: Colors.black54,
),
),
labelText: 'Password',
),
),
),
Column(
children: <Widget>[
Padding(
padding: EdgeInsets.symmetric(vertical: 10.0),
child: Container(
decoration: BoxDecoration(
color: Colors.blue,
borderRadius: BorderRadius.circular(10.0),
),
width: 350.0,
child: FlatButton(
onPressed: null,
child: Text(
'Sign Up',
style: TextStyle(
color: Colors.white,
fontSize: 18.0,
),
),
),
),
),
Padding(
padding: EdgeInsets.symmetric(vertical: 10.0),
child: Container(
decoration: BoxDecoration(
color: Colors.red,
borderRadius: BorderRadius.circular(10.0),
),
width: 350.0,
child: FlatButton(
onPressed: _submit,
child: Text(
'Back to Log In',
style: TextStyle(
color: Colors.white,
fontSize: 18.0,
),
),
),
),
),
],
),
],
),
),
),
),
),
);
}
}
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: SignupForm(),
);
}
}
答案 1 :(得分:0)
您正在调用方法,而不是设置回调。编译器通常会选择此方法,但是不会键入您的方法。 Dart中的无类型方法会返回一个动态对象,该对象可能是一个函数,这就是为什么您可以从小部件中调用它的原因。应该通过将其更改为onPressed: _submit
或onPressed: () => _submit()
来修复它,因为它们是函数而不是方法调用。