我目前正在做一个颤振项目。我在底部做了一个平面按钮,并将函数 _submit() 作为 onPressed 方法。但似乎 _formKey.currentState.validate() 有错误。错误信息如下图所示
<块引用>'validate' 方法在 null 上被调用。 接收器:空 尝试调用:validate()
我发现了与我类似的问题并试图解决它。但我找不到我的错误。
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
class AddTaskScreen extends StatefulWidget {
AddTaskScreen({Key key}) : super(key: key);
@override
_AddTaskScreenState createState() => _AddTaskScreenState();
}
class _AddTaskScreenState extends State<AddTaskScreen> {
final _formKey = GlobalKey<FormState>();
String _title = '';
String _priority;
DateTime _date = DateTime.now();
TextEditingController _dateController = TextEditingController();
final DateFormat _dateFormatter = DateFormat('MMM dd, yyy');
final List<String> _priorities = ['Low', 'Medium', 'High'];
_handleDatePicker() async {
final DateTime date = await showDatePicker(
context: context,
initialDate: _date,
firstDate: DateTime(2000),
lastDate: DateTime(2100));
if (date != null && date != _date) {
setState(() {
_date = date;
});
_dateController.text = _dateFormatter.format(date);
}
}
_submit() {
if (_formKey.currentState.validate()) {
_formKey.currentState.save();
print('$_title $_date $_priority');
Navigator.pop(context);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: GestureDetector(
onTap: () => FocusScope.of(context).unfocus(),
child: SingleChildScrollView(
child: Container(
padding: EdgeInsets.symmetric(horizontal: 40.0, vertical: 80.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
GestureDetector(
onTap: () => Navigator.pop(context),
child: Icon(
Icons.arrow_back_ios,
size: 30.0,
color: Colors.black,
),
),
SizedBox(
height: 20.0,
),
Text('Add Task',
style: TextStyle(
color: Colors.black,
fontSize: 40.0,
fontWeight: FontWeight.bold)),
SizedBox(height: 10.0),
Form(
key: _formKey,
child: Column(
children: <Widget>[
Padding(
padding: EdgeInsets.symmetric(vertical: 20.0),
child: TextFormField(
style: TextStyle(fontSize: 18.0),
decoration: InputDecoration(
labelText: 'Title',
labelStyle: TextStyle(fontSize: 18.0),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10.0))),
validator: (input) => input.trim().isEmpty
? 'Please enter a task title'
: null,
onSaved: (input) => _title = input,
initialValue: _title),
),
Padding(
padding: EdgeInsets.symmetric(vertical: 20.0),
child: TextFormField(
readOnly: true,
controller: _dateController,
style: TextStyle(fontSize: 18.0),
onTap: _handleDatePicker,
decoration: InputDecoration(
labelText: 'Date',
labelStyle: TextStyle(fontSize: 18.0),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10.0))),
)),
Padding(
padding: EdgeInsets.symmetric(vertical: 20.0),
child: DropdownButtonFormField(
icon: Icon(Icons.arrow_drop_down_circle),
iconSize: 22.0,
items: _priorities.map((String priority) {
return DropdownMenuItem(
value: priority,
child: Text(
priority,
style: TextStyle(
color: Colors.black, fontSize: 18.0),
),
);
}).toList(),
style: TextStyle(fontSize: 18.0),
decoration: InputDecoration(
labelText: 'Priority',
labelStyle: TextStyle(fontSize: 18.0),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10.0))),
validator: (input) => _priority == null
? 'Please select a priority level'
: null,
onChanged: (value) {
setState(() {
_priority = value;
});
},
value: _priority,
)),
Container(
margin: EdgeInsets.symmetric(vertical: 20.0),
height: 60.0,
width: double.infinity,
decoration: BoxDecoration(
color: Colors.black,
borderRadius: BorderRadius.circular(30.0)),
child: FlatButton(
onPressed: _submit(),
child: Text(
'Add',
style: TextStyle(
color: Colors.white, fontSize: 20.0),
)),
)
],
),
)
],
),
),
),
),
);
}
}
答案 0 :(得分:2)
您可以在下面复制粘贴运行完整代码
您可以将 _submit()
更改为 _submit
因为 _submit()
表示执行函数
代码片段更改自
FlatButton(
onPressed: _submit(),
到
FlatButton(
onPressed: _submit,
工作演示
完整代码
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
class AddTaskScreen extends StatefulWidget {
AddTaskScreen({Key key}) : super(key: key);
@override
_AddTaskScreenState createState() => _AddTaskScreenState();
}
class _AddTaskScreenState extends State<AddTaskScreen> {
final _formKey = GlobalKey<FormState>();
String _title = '';
String _priority;
DateTime _date = DateTime.now();
TextEditingController _dateController = TextEditingController();
final DateFormat _dateFormatter = DateFormat('MMM dd, yyy');
final List<String> _priorities = ['Low', 'Medium', 'High'];
_handleDatePicker() async {
final DateTime date = await showDatePicker(
context: context,
initialDate: _date,
firstDate: DateTime(2000),
lastDate: DateTime(2100));
if (date != null && date != _date) {
setState(() {
_date = date;
});
_dateController.text = _dateFormatter.format(date);
}
}
_submit() {
if (_formKey.currentState.validate()) {
_formKey.currentState.save();
print('$_title $_date $_priority');
Navigator.pop(context);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: GestureDetector(
onTap: () => FocusScope.of(context).unfocus(),
child: SingleChildScrollView(
child: Container(
padding: EdgeInsets.symmetric(horizontal: 40.0, vertical: 80.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
GestureDetector(
onTap: () => Navigator.pop(context),
child: Icon(
Icons.arrow_back_ios,
size: 30.0,
color: Colors.black,
),
),
SizedBox(
height: 20.0,
),
Text('Add Task',
style: TextStyle(
color: Colors.black,
fontSize: 40.0,
fontWeight: FontWeight.bold)),
SizedBox(height: 10.0),
Form(
key: _formKey,
child: Column(
children: <Widget>[
Padding(
padding: EdgeInsets.symmetric(vertical: 20.0),
child: TextFormField(
style: TextStyle(fontSize: 18.0),
decoration: InputDecoration(
labelText: 'Title',
labelStyle: TextStyle(fontSize: 18.0),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10.0))),
validator: (input) => input.trim().isEmpty
? 'Please enter a task title'
: null,
onSaved: (input) => _title = input,
initialValue: _title),
),
Padding(
padding: EdgeInsets.symmetric(vertical: 20.0),
child: TextFormField(
readOnly: true,
controller: _dateController,
style: TextStyle(fontSize: 18.0),
onTap: _handleDatePicker,
decoration: InputDecoration(
labelText: 'Date',
labelStyle: TextStyle(fontSize: 18.0),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10.0))),
)),
Padding(
padding: EdgeInsets.symmetric(vertical: 20.0),
child: DropdownButtonFormField(
icon: Icon(Icons.arrow_drop_down_circle),
iconSize: 22.0,
items: _priorities.map((String priority) {
return DropdownMenuItem(
value: priority,
child: Text(
priority,
style: TextStyle(
color: Colors.black, fontSize: 18.0),
),
);
}).toList(),
style: TextStyle(fontSize: 18.0),
decoration: InputDecoration(
labelText: 'Priority',
labelStyle: TextStyle(fontSize: 18.0),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10.0))),
validator: (input) => _priority == null
? 'Please select a priority level'
: null,
onChanged: (value) {
setState(() {
_priority = value;
});
},
value: _priority,
)),
Container(
margin: EdgeInsets.symmetric(vertical: 20.0),
height: 60.0,
width: double.infinity,
decoration: BoxDecoration(
color: Colors.black,
borderRadius: BorderRadius.circular(30.0)),
child: FlatButton(
onPressed: _submit,
child: Text(
'Add',
style: TextStyle(
color: Colors.white, fontSize: 20.0),
)),
)
],
),
)
],
),
),
),
),
);
}
}
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: AddTaskScreen(),
);
}
}