setter 'firstName=' 被调用为 null。接收者:null 尝试调用:firstName="hfjfhd" in flutter
The setter 'firstName=' was called on null. Receiver: null Tried calling: firstName="hfjfhd" in flutter
请有人帮助我,自从过去两天以来,我一直遇到这个错误,我正在尝试使用单击按钮从输入表单发送数据到列表,单击按钮时,表单打开同一页面,但是当我在表单中输入数据并单击提交按钮时,没有任何反应,并且在终端上显示 The setter 'firstName=' was called on null。接收者:null 尝试调用:firstName="hfjfhd".
这是我的 model.dart 文件
class Model { string firstName = ""; String lastName = ""; String email = ""; String password = ""; Model({this.firstName, this.lastName, this.email, this.password});}
这是我打开表格并显示结果的主文件。
import 'package:flutter/material.dart';
import 'model.dart';
import 'package:dummy_project/components/save_data_from_input_into_object/form.dart';
import 'package:dummy_project/components/save_data_from_input_into_object/result.dart';
class DialogForm extends StatefulWidget {
List<Model> models = <Model>[];
Model tempModel;
DialogForm();
@override
State<DialogForm> createState() => _ResultState();
@override
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
super.debugFillProperties(properties);
properties.add(DiagnosticsProperty<Model>('model', tempModel));
}
}
class _ResultState extends State<DialogForm> {
final _formKey = GlobalKey<FormState>();
@override
Widget build(BuildContext context) {
return (Scaffold(
appBar: AppBar(title: Text('Successful')),
body: Container(
margin: EdgeInsets.all(10.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Flexible(
child: ListView.builder(
itemCount: widget.models.length,
itemBuilder: (context, index) {
return ListTile(
title: Text(widget.models[index].firstName),
subtitle: Text(widget.models[index].lastName),
trailing: Text(widget.models[index].email),
);
},
),
),
Align(
child: RaisedButton(
child: Text('Click Me!'),
onPressed: (){
showDialog(
context: context,
builder: (BuildContext context){
return AlertDialog(
content: Stack(
overflow: Overflow.visible,
children: <Widget>[
Positioned(
right: -40,
top: -40,
child: InkResponse(
onTap: () {
Navigator.of(context).pop();
},
child: CircleAvatar(
child: Icon(Icons.close),
backgroundColor: Colors.red,
),
),
),
Form(
key: _formKey,
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Padding(
padding: EdgeInsets.all(8.0),
child: MyTextFormField(
hintText: 'First Name',
validator: (String value) {
if (value.isEmpty) {
return 'Enter your first name';
}
return null;
},
onSaved: (String value) {
widget.tempModel.firstName = value;
},
),
),
Padding(
padding: EdgeInsets.all(8.0),
child: MyTextFormField(
hintText: 'Last Name',
validator: (String value) {
if (value.isEmpty) {
return 'Enter your last name';
}
return null;
},
onSaved: (String value) {
widget.tempModel.lastName = value;
},
),
),
Padding(
padding: EdgeInsets.all(8.0),
child: MyTextFormField(
hintText: 'Enter email',
isEmail: true,
// validator: (String value) {
// if (!validator.isEmail(value)) {
// return 'Please enter a valid email';
// }
// return null;
// },
onSaved: (String value) {
widget.tempModel.email = value;
},
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: RaisedButton(
child: Text("Submit"),
onPressed: () {
if (_formKey.currentState.validate()) {
_formKey.currentState.save();
widget.models.add(widget.tempModel);
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => DialogForm()));
}
},
),
)
],
)
),
],
),
);
}
);
}
),
alignment: Alignment.bottomRight,
),
],
),
),
)
);
}
}
class MyTextFormField extends StatelessWidget {
final String hintText;
final Function validator;
final Function onSaved;
final bool isPassword;
final bool isEmail;
MyTextFormField({
this.hintText,
this.validator,
this.onSaved,
this.isPassword = false,
this.isEmail = false,
});
@override
Widget build(BuildContext context) {
return Padding(
padding: EdgeInsets.all(8.0),
child: TextFormField(
decoration: InputDecoration(
hintText: hintText,
contentPadding: EdgeInsets.all(15.0),
border: InputBorder.none,
filled: true,
fillColor: Colors.grey[200],
),
obscureText: isPassword ? true : false,
validator: validator,
onSaved: onSaved,
keyboardType: isEmail ? TextInputType.emailAddress : TextInputType.text,
),
);
}
}```
使其可为空:“?”这里声明它可以默认为 null 或通过任何操作。
建议:使用 textEditingController 获取输入值,并传递它。
并在显示模型中的文本时使用 null 安全检查,因为您将其定义为 null。
widget.models[index].firstName ?? "First Name"
以上,“??”意味着如果左语句在任何情况下都是空的,那么右语句将被执行,所以如果你在你的文本字段中得到“名字”然后它从模型中得到空。
class Model{
String? firstName;
String? lastName;
String? email;
String? password;
Model({
this.firstName,
this.lastName,
this.email,
this.password
});
}
如果您有任何疑问,请告诉我
谢谢
您没有初始化 tempModel
,因此它将为空,这就是为什么您在 widget.tempModel.firstName
.
处收到错误消息的原因
而不是 Model tempModel;
试试:
Model tempModel = Model();
或
final tempModel = Model();
请有人帮助我,自从过去两天以来,我一直遇到这个错误,我正在尝试使用单击按钮从输入表单发送数据到列表,单击按钮时,表单打开同一页面,但是当我在表单中输入数据并单击提交按钮时,没有任何反应,并且在终端上显示 The setter 'firstName=' was called on null。接收者:null 尝试调用:firstName="hfjfhd".
这是我的 model.dart 文件
class Model { string firstName = ""; String lastName = ""; String email = ""; String password = ""; Model({this.firstName, this.lastName, this.email, this.password});}
这是我打开表格并显示结果的主文件。
import 'package:flutter/material.dart';
import 'model.dart';
import 'package:dummy_project/components/save_data_from_input_into_object/form.dart';
import 'package:dummy_project/components/save_data_from_input_into_object/result.dart';
class DialogForm extends StatefulWidget {
List<Model> models = <Model>[];
Model tempModel;
DialogForm();
@override
State<DialogForm> createState() => _ResultState();
@override
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
super.debugFillProperties(properties);
properties.add(DiagnosticsProperty<Model>('model', tempModel));
}
}
class _ResultState extends State<DialogForm> {
final _formKey = GlobalKey<FormState>();
@override
Widget build(BuildContext context) {
return (Scaffold(
appBar: AppBar(title: Text('Successful')),
body: Container(
margin: EdgeInsets.all(10.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Flexible(
child: ListView.builder(
itemCount: widget.models.length,
itemBuilder: (context, index) {
return ListTile(
title: Text(widget.models[index].firstName),
subtitle: Text(widget.models[index].lastName),
trailing: Text(widget.models[index].email),
);
},
),
),
Align(
child: RaisedButton(
child: Text('Click Me!'),
onPressed: (){
showDialog(
context: context,
builder: (BuildContext context){
return AlertDialog(
content: Stack(
overflow: Overflow.visible,
children: <Widget>[
Positioned(
right: -40,
top: -40,
child: InkResponse(
onTap: () {
Navigator.of(context).pop();
},
child: CircleAvatar(
child: Icon(Icons.close),
backgroundColor: Colors.red,
),
),
),
Form(
key: _formKey,
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Padding(
padding: EdgeInsets.all(8.0),
child: MyTextFormField(
hintText: 'First Name',
validator: (String value) {
if (value.isEmpty) {
return 'Enter your first name';
}
return null;
},
onSaved: (String value) {
widget.tempModel.firstName = value;
},
),
),
Padding(
padding: EdgeInsets.all(8.0),
child: MyTextFormField(
hintText: 'Last Name',
validator: (String value) {
if (value.isEmpty) {
return 'Enter your last name';
}
return null;
},
onSaved: (String value) {
widget.tempModel.lastName = value;
},
),
),
Padding(
padding: EdgeInsets.all(8.0),
child: MyTextFormField(
hintText: 'Enter email',
isEmail: true,
// validator: (String value) {
// if (!validator.isEmail(value)) {
// return 'Please enter a valid email';
// }
// return null;
// },
onSaved: (String value) {
widget.tempModel.email = value;
},
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: RaisedButton(
child: Text("Submit"),
onPressed: () {
if (_formKey.currentState.validate()) {
_formKey.currentState.save();
widget.models.add(widget.tempModel);
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => DialogForm()));
}
},
),
)
],
)
),
],
),
);
}
);
}
),
alignment: Alignment.bottomRight,
),
],
),
),
)
);
}
}
class MyTextFormField extends StatelessWidget {
final String hintText;
final Function validator;
final Function onSaved;
final bool isPassword;
final bool isEmail;
MyTextFormField({
this.hintText,
this.validator,
this.onSaved,
this.isPassword = false,
this.isEmail = false,
});
@override
Widget build(BuildContext context) {
return Padding(
padding: EdgeInsets.all(8.0),
child: TextFormField(
decoration: InputDecoration(
hintText: hintText,
contentPadding: EdgeInsets.all(15.0),
border: InputBorder.none,
filled: true,
fillColor: Colors.grey[200],
),
obscureText: isPassword ? true : false,
validator: validator,
onSaved: onSaved,
keyboardType: isEmail ? TextInputType.emailAddress : TextInputType.text,
),
);
}
}```
使其可为空:“?”这里声明它可以默认为 null 或通过任何操作。
建议:使用 textEditingController 获取输入值,并传递它。 并在显示模型中的文本时使用 null 安全检查,因为您将其定义为 null。
widget.models[index].firstName ?? "First Name"
以上,“??”意味着如果左语句在任何情况下都是空的,那么右语句将被执行,所以如果你在你的文本字段中得到“名字”然后它从模型中得到空。
class Model{
String? firstName;
String? lastName;
String? email;
String? password;
Model({
this.firstName,
this.lastName,
this.email,
this.password
});
}
如果您有任何疑问,请告诉我 谢谢
您没有初始化 tempModel
,因此它将为空,这就是为什么您在 widget.tempModel.firstName
.
而不是 Model tempModel;
试试:
Model tempModel = Model();
或
final tempModel = Model();