我正在学习Flutter编程。我现在正在制作一个简单的注册表格,仅包含2个字段,即名称和注册名。我已经成功地使用db创建了一个简单的笔记制作应用程序,因此遵循此处的相同说明。但是遇到错误“未处理的异常:NoSuchMethodError:设置程序'regday ='被调用为null。”
注册模型类
class RegistrationModel
{
int _id;
String _name;
String _dob;
String _regDay;
RegistrationModel(this._name, this._dob, [this._regDay]);
RegistrationModel.withId(this._id, this._name, this._dob, [this._regDay]);
//GETTERS
int get id => _id;
String get name => _name;
String get dob => _dob;
String get regday => _regDay;
//SETTER
set name(String newName)
{
if(newName.length <= 30)
{
this._name = newName.trim();
}
}
set dob(String newDob)
{
this._dob = newDob;
}
set regday(String newRegDay)
{
print("New Reg Day : $newRegDay");
this._regDay = newRegDay;
}
//CONVERT REG OBJ TO MAP
Map<String, dynamic> toMap()
{
var map = Map<String, dynamic>();
if(id != null)
{
map['id'] = _id;
}
map['name'] = _name;
map['dob'] = _dob;
map['regday'] = _regDay;
return map;
}
//CONVERT MAP TO REG
RegistrationModel.fromMapObject(Map<String, dynamic> map){
this._id = map['id'];
this._name = map['name'];
this._dob = map['dob'];
this._regDay = map['regday'];
}
}
设置值的代码
class Registration extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return RegistrationState();
}
}
class RegistrationState extends State {
RegistrationModel reg;
DatabaseHelper databaseHelper = DatabaseHelper();
var _minimumPadding = 5.0;
var _formKey;
TextEditingController _nameController = new TextEditingController();
TextEditingController _dobController = new TextEditingController();
@override
Widget build(BuildContext context) {
TextStyle textStyle = Theme.of(context).textTheme.title;
// _nameController.text = reg.name;
// _dobController.text = reg.dob;
//DATEPICKER
DateTime _selectedDate = DateTime.now();
Future<Null> _selectDate(BuildContext context) async {
final DateTime picked = await showDatePicker(
context: context,
initialDate: _selectedDate,
firstDate: DateTime(1990),
lastDate: DateTime(2101));
if (picked != null && picked != _selectedDate) {
print("Selected Date in the func : $picked");
setState(() {
_selectedDate = picked;
_dobController.text = (picked.day).toString() +
"/" +
(picked.month).toString() +
"/" +
(picked.year).toString();
});
// Focus.clear(context);
}
}
return Scaffold(
appBar: AppBar(
title: Text("Register"),
),
body: Form(
key: _formKey,
child: Padding(
padding: EdgeInsets.only(
top: _minimumPadding * 2,
left: _minimumPadding * 2,
right: _minimumPadding * 2,
),
child: ListView(
children: <Widget>[
//NAME
TextField(
controller: _nameController,
style: textStyle,
onChanged: (value) {
updateName();
},
decoration: InputDecoration(
labelStyle: textStyle,
labelText: "Name",
icon: Icon(Icons.person),
),
),
//DATE OF BIRTH
TextField(
controller: _dobController,
keyboardType: TextInputType.datetime,
onChanged: (value) {
updateDob();
},
decoration: InputDecoration(
labelStyle: textStyle,
labelText: "Date Of Birth",
icon: Icon(Icons.calendar_today),
),
onTap: () {
_selectDate(context);
},
),
RaisedButton(
child: Text(
"Register",
textScaleFactor: 1.5,
),
color: Theme.of(context).primaryColor,
textColor: Theme.of(context).primaryColorLight,
onPressed: () {
_save();
print("Save button pressed");
},
),
],
),
),
),
);
}
void _save() async {
String today = DateFormat.yMMMd().format(DateTime.now());
print("Today : $today");
reg.regday = today;
reg.name = _nameController.text;
reg.dob = _dobController.text;
int result;
if (reg.id != null) //UPDATE
{
result = await databaseHelper.updateRegister(reg);
} else {
result = await databaseHelper.inserRegister(reg);
}
if (result != 0) //SUCCESS
{
_showAlertDialog("Success", "Registered successfully");
} else //FAILED
{
_showAlertDialog("Failed", "Problem in registering");
}
}
跟踪
[ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: NoSuchMethodError: The setter 'regday=' was called on null.
E/flutter (21661): Receiver: null
E/flutter (21661): Tried calling: regday="Apr 1, 2019"
E/flutter (21661): #0 Object.noSuchMethod (dart:core/runtime/libobject_patch.dart:50:5)
E/flutter (21661): #1 RegistrationState._save (package:registration_test/screens/registration_screen.dart:136:9)
E/flutter (21661): <asynchronous suspension>
E/flutter (21661): #2 RegistrationState.build.<anonymous closure> (package:registration_test/screens/registration_screen.dart:111:19)
E/flutter (21661): #3 _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:513:14)
E/flutter (21661): #4 _InkResponseState.build.<anonymous closure> (package:flutter/src/material/ink_well.dart:568:30)
E/flutter (21661): #5 GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:120:24)
E/flutter (21661): #6 TapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:242:9)
E/flutter (21661): #7 TapGestureRecognizer.acceptGesture (package:flutter/src/gestures/tap.dart:204:7)
E/flutter (21661): #8 GestureArenaManager.sweep (package:flutter/src/gestures/arena.dart:156:27)
E/flutter (21661): #9 _WidgetsFlutterBinding&BindingBase&GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:218:20)
E/flutter (21661): #10 _WidgetsFlutterBinding&BindingBase&GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:192:22)
E/flutter (21661): #11 _WidgetsFlutterBinding&BindingBase&GestureBinding._handlePointerEvent (package:flutter/src/gestures/binding.dart:149:7)
E/flutter (21661): #12 _WidgetsFlutterBinding&BindingBase&GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:101:7)
E/flutter (21661): #13 _WidgetsFlutterBinding&BindingBase&GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:85:7)
E/flutter (21661): #14 _rootRunUnary (dart:async/zone.dart:1136:13)
E/flutter (21661): #15 _CustomZone.runUnary (dart:async/zone.dart:1029:19)
E/flutter (21661): #16 _CustomZone.runUnaryGuarded (dart:async/zone.dart:931:7)
E/flutter (21661): #17 _invoke1 (dart:ui/hooks.dart:223:10)
E/flutter (21661): #18 _dispatchPointerDataPacket (dart:ui/hooks.dart:144:5)
预期输出将是显示成功消息的警报框,但会引发此错误。任何帮助将不胜感激。
答案 0 :(得分:1)
那是因为您的reg对象为null。在_save函数的第一行中添加
reg = RegistrationModel('','');
答案 1 :(得分:0)
尝试
RegistrationModel reg = RegistrationModel();