I am getting this error when I click the register button from login page. I want to register the user and then login the user as per email id and password. I don't know what to do or what is wrong.
I have UserLoginModel.dart
, db_helper.dart
, Login_ctr.dart
files.
Error in this line:
firstnameController.text = userLoginModel.firstName;
UserRegistration.dart
import 'package:customer/models/UserLoginModel.dart';
import 'package:customer/screens/add_person.dart';
import 'package:customer/utils/db_helper.dart';
import 'package:flutter/material.dart';
import 'package:customer/screens/UserLogin.dart';
import 'package:customer/utils/Login_ctr.dart';
class UserRegistration extends StatefulWidget{
final UserLoginModel userLoginModel;
//const UserRegistration({Key key, this.userLoginModel}) : super(key: key);
UserRegistration(this. userLoginModel);
@override
State<StatefulWidget> createState() {
return UserRegistrationState();
}
}
class UserRegistrationState extends State<UserRegistration>{
DatabaseHelper helper = DatabaseHelper();
UserLoginModel userLoginModel;
var _minimumPadding = 5.0;
bool isHiddenPassword=true;
final GlobalKey<FormState> _formkey = GlobalKey<FormState>();
TextEditingController firstnameController=TextEditingController();
TextEditingController lastnameController=TextEditingController();
TextEditingController passwordController=TextEditingController();
TextEditingController cpasswordController=TextEditingController();
TextEditingController emailController=TextEditingController();
TextEditingController mobilenoController=TextEditingController();
TextEditingController addressController=TextEditingController();
@override
Widget build(BuildContext context) {
TextStyle textStyle=Theme.of(context).textTheme.title;
var height = MediaQuery.of(context).size.height;
var width = MediaQuery.of(context).size.width;
firstnameController.text = userLoginModel.firstName;
lastnameController.text = userLoginModel.lastName;
passwordController.text = userLoginModel.password;
emailController.text = userLoginModel.emailId;
mobilenoController.text = userLoginModel.mobileNum;
addressController.text = userLoginModel.address;
return Scaffold(
backgroundColor: Colors.white,
appBar: AppBar(
title: Text('Registration'),),
body:Form(
key: _formkey,
child:ListView(
children: <Widget>[
Text("Registration".toUpperCase(),),
Padding(
padding: EdgeInsets.all(_minimumPadding*3),
child: TextFormField(
controller: firstnameController,
style:textStyle,
validator: (String value){
if(value.isEmpty)
{
return 'Please Enter First Name';
}
return null;
},
onChanged: (String value){
//name = value;
insertuser();
},
decoration: InputDecoration(
prefixIcon: Icon(Icons.person),
labelText: 'FirstName',
hintText: 'FirstName',
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(25.0),
),)
),
),
Padding(
padding: EdgeInsets.all(_minimumPadding*3),
child: TextFormField(
controller: lastnameController,
style:textStyle,
validator: (String value){
if(value.isEmpty)
{
return 'Please Enter Last Name';
}
return null;
},
onSaved: (String value){
userLoginModel.last_name = lastnameController.text;
},
decoration: InputDecoration(
prefixIcon: Icon(Icons.person),
labelText: 'Last name',
hintText: 'Last name',)
),
),
Padding(
padding: EdgeInsets.all(_minimumPadding*3),
child:TextFormField(
controller: passwordController,
style:textStyle,
validator: (String value){
if(value.isEmpty)
{
return 'Please a Enter Password';
}
return null;
},
obscureText:isHiddenPassword,
decoration: InputDecoration(
prefixIcon: Icon(Icons.lock),
suffixIcon: InkWell(
onTap:_togglePasswordView,
child: Icon(Icons.visibility)),
labelText: 'Password',
hintText: 'Password',
)
)),
Padding(
padding: EdgeInsets.all(_minimumPadding*3), child:Expanded(
child:TextFormField(
controller: cpasswordController,
style:textStyle,
obscureText:true,
validator: (String value){
if(value.isEmpty)
{
return 'Please re-enter password';
}
print(passwordController.text);
print(cpasswordController.text);
if(passwordController.text!=cpasswordController.text){
return "Password does not match";
}
return null;
},
onSaved: (String value){
userLoginModel.password = passwordController.text;
},
decoration: InputDecoration(
prefixIcon: Icon(Icons.lock),
suffixIcon: Icon(Icons.visibility),
labelText: 'Confirm password',
hintText: 'Confirm password',
)
))),
Padding(
padding: EdgeInsets.all(_minimumPadding*3),
child:TextFormField(
controller: emailController,
style:textStyle,
validator: (String value){
if(value.isEmpty)
{
return 'Please Enter E-mail address';
}
if(!RegExp("^[a-zA-Z0-9+_.-]+@[a-zA-Z0-9.-]+.[a-z]").hasMatch(value)){
return 'Please enter a valid Email';
}
return null;
},
onSaved: (String value){
userLoginModel.email_id = emailController.text;
},
decoration: InputDecoration(
prefixIcon: Icon(Icons.mail),
labelText: 'Email',
hintText: 'Email',
)
)),
Padding(
padding: EdgeInsets.all(_minimumPadding*3),
child:TextFormField(
controller: mobilenoController,
style:textStyle,
validator: (String value){
if(value.isEmpty)
{
return 'Please enter phone no.';
}
if(value.length<10)
{
return 'Please enter a valid phone no.';
}
return null;
},
onSaved: (String value){
userLoginModel.mobile_num = mobilenoController.text;
},
decoration: InputDecoration(
prefixIcon: Icon(Icons.phone),
labelText: 'Mobile number',
hintText: 'Mobile number',
)
)),
Padding(
padding: EdgeInsets.all(_minimumPadding*3),
child: TextFormField(
controller: addressController,
style:textStyle,
validator: (String value){
if(value.isEmpty)
{
return 'Please Enter Address';
}
return null;
},
onSaved: (String value){
userLoginModel.address = addressController.text;
},
decoration: InputDecoration(
prefixIcon: Icon(Icons.person),
labelText: 'Address',
hintText: 'Address',
)
),
),
GestureDetector(
onTap: (){
if(_formkey.currentState.validate())
{
print("successful");
Navigator.push(context,
MaterialPageRoute(builder: (context) => AddPerson()));
return;
}else{
print("UnSuccessfull");
}
},
child: GestureDetector(
onTap: () {
_save();
Navigator.push(context,
MaterialPageRoute(builder: (context) => UserLogin()));
},
child: Center(
child: Container(
padding:
EdgeInsets.symmetric(horizontal: 26, vertical: 20),
child: Text(
"Register".toUpperCase(),
),
),
),
),
),
Padding(
padding: EdgeInsets.all(_minimumPadding*3),
child: Row(
children: <Widget>[
Expanded(
child: Text("Already registered?",
)
),
GestureDetector(
onTap: () {
Navigator.push(context,
MaterialPageRoute(builder: (context) => UserLogin()));
},
child:Center(
child: Container(
padding:
EdgeInsets.symmetric(horizontal: 26, vertical: 10),
child: Text(
"Login".toUpperCase(),
),
),
),
)
],
)
)
],
)));
}
void _togglePasswordView(){
setState(() {
isHiddenPassword=!isHiddenPassword;
});
}
void moveToLastScreen() {
Navigator.pop(context, true);
}
void insertuser() {
userLoginModel.first_name = firstnameController.text;
}
void insertpassword() {
userLoginModel.password = passwordController.text;
}
void _save() async {
LoginCtr con = new LoginCtr();
moveToLastScreen();
int result;
if (userLoginModel.user_id != null) { // Case 1: Update operation
//result = await helper.updateUser(note);
} else { // Case 2: Insert Operation
result = await con.saveUser(userLoginModel);
}
if (result != 0) { // Success
_showAlertDialog('Status', 'Note Saved Successfully');
} else { // Failure
_showAlertDialog('Status', 'Problem Saving Note');
}
}
void _showAlertDialog(String title, String message) {
AlertDialog alertDialog = AlertDialog(
title: Text(title),
content: Text(message),
);
showDialog(
context: context,
builder: (_) => alertDialog
);
}
}
UserLoginModel.dart
class UserLoginModel {
int _user_id;
String _firstName,_lastName,_mobileNum,_emailId,_address,_userType;
String _password;
UserLoginModel(this._firstName,this._lastName,this._mobileNum,this._emailId,this._address,this._password,this._userType);
UserLoginModel.fromMap(dynamic obj) {
this._user_id=obj['user_id'];
this._firstName = obj['first_name'];
this._lastName = obj['last_name'];
this._emailId = obj['email_id'];
this._mobileNum = obj['mobile_num'];
this._address = obj['address'];
this._password = obj['password'];
this._userType = obj['user_type'];
}
int get user_id => _user_id;
String get user_type => _userType;
String get firstName => null;
String get lastName => null;
String get password => null;
String get emailId => null;
String get mobileNum => null;
String get address => null;
set first_name(String fname) {
this._firstName = fname;
}
set last_name(String lname) {
this._lastName = lname;
}
set email_id(String email) {
this._emailId = email;
}
set mobile_num(String mob) {
this._mobileNum = mob;
}
set address(String add) {
this._address = add;
}
set password(String pwd) {
this._password = pwd;
}
Map<String, dynamic> toMap() {
var map = new Map<String, dynamic>();
if (user_id != null) {
map['user_id'] = user_id;
}
map["first_name"] = _firstName;
map["last_name"] = _lastName;
map["email_id"] = _emailId;
map["mobile_num"] = _mobileNum;
map["address"] = _address;
map["password"] = _password;
map["user_type"] = _userType;
return map;
}
}
UserLogin.dart
import 'dart:ui';
import 'package:customer/models/UserLoginModel.dart';
import 'package:customer/utils/Login_ctr.dart';
import 'package:customer/utils/db_helper.dart';
import 'package:customer/utils/login_response.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:sqflite/sqflite.dart';
import 'Add_people.dart';
import 'UserRegistration.dart';
import 'people_list.dart';
import 'package:flutter/material.dart';
import 'ForgotPassword.dart';
class UserLogin extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return UserLoginState();
}
}
enum LoginStatus { notSignIn, signIn }
class UserLoginState extends State<UserLogin> implements LoginCallBack {
DatabaseHelper databaseHelper = DatabaseHelper();
String name,_password,_email_id;
var _formKey=GlobalKey<FormState>();
var _minimumPadding = 5.0;
TextEditingController usernameController=TextEditingController();
TextEditingController passwordController=TextEditingController();
bool isHiddenPassword=true;
LoginStatus _loginStatus = LoginStatus.notSignIn;
BuildContext _ctx;
bool _isLoading = false;
final scaffoldKey = new GlobalKey<ScaffoldState>();
LoginResponse _response;
UserLoginState() {
_response = new LoginResponse(this);
}
void _submit() {
final form = _formKey.currentState;
if (form.validate()) {
setState(() {
_isLoading = true;
form.save();
_response.doLogin(_email_id, _password);
});
}
}
void _showSnackBar(String text) {
scaffoldKey.currentState.showSnackBar(new SnackBar(
content: new Text(text),
));
}
var value;
getPref() async {
SharedPreferences preferences = await SharedPreferences.getInstance();
setState(() {
value = preferences.getInt("value");
_loginStatus = value == 1 ? LoginStatus.signIn : LoginStatus.notSignIn;
});
}
signOut() async {
SharedPreferences preferences = await SharedPreferences.getInstance();
setState(() {
preferences.setInt("value", null);
preferences.commit();
_loginStatus = LoginStatus.notSignIn;
});
}
@override
void initState() {
super.initState();
getPref();
}
void _togglePasswordView() {
setState(() {
isHiddenPassword = !isHiddenPassword;
});
}
@override
Widget build(BuildContext context) {
TextStyle textStyle = Theme.of(context).textTheme.title;
var height = MediaQuery.of(context).size.height;
var width = MediaQuery.of(context).size.width;
switch (_loginStatus) {
case LoginStatus.notSignIn:
_ctx = context;
return Scaffold(
backgroundColor: Colors.white,
appBar: AppBar(
title: Text('Customer Tracking System'),),
body:
Form(
key: _formKey,
child: Container(
child: Padding(
padding: EdgeInsets.all(_minimumPadding * 2),
child: ListView
(
children: <Widget>[
Text("Login".toUpperCase(),),
Padding(
padding: EdgeInsets.only(
top: _minimumPadding * 3,
bottom: _minimumPadding),
child: TextFormField(
controller: usernameController,
style: textStyle,
validator: (String value) {
if (value.isEmpty) {
return 'Please Enter Name';
}
return null;
},
onSaved: (String value) {
_email_id = value;
},
decoration: InputDecoration(
prefixIcon: Icon(Icons.person),
labelText: 'Username',
hintText: 'Username',
)
)),
Padding(
padding: EdgeInsets.only(
top: _minimumPadding * 3,
bottom: _minimumPadding),
child: TextFormField(
style: textStyle,
obscureText: isHiddenPassword,
controller: passwordController,
validator: (String value) {
if (value.isEmpty) {
return 'Please Enter Name';
}
return null;
},
onSaved: (String value) {
_password = value;
},
decoration: InputDecoration(
prefixIcon: Icon(Icons.lock),
suffixIcon: InkWell(
onTap: _togglePasswordView,
child: Icon(Icons.visibility)),
labelText: 'Password',
hintText: 'Password',
)
)
), InkWell(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => ForgotPassword()));
},
child: Text(
"Forgot Password ?",
),
),
GestureDetector(
onTap: () {
_submit();
},
child: Container(
padding:
EdgeInsets.symmetric(horizontal: 26, vertical: 20),
child: Text(
"Login".toUpperCase(),
),
),
),
Padding(
padding: EdgeInsets.only(
top: _minimumPadding * 2,
bottom: _minimumPadding),
child: Row(
children: <Widget>[
Expanded(
child: Text("Not yet registered?",
),
GestureDetector(
onTap: () {
navigateToRegistration(UserLoginModel('','','','','','',''));
},
child: Center(
child: Container(
padding:
EdgeInsets.symmetric(
horizontal: 26, vertical: 10),
child: Text(
"Register".toUpperCase(),
),
),
),
)
],
)
),
],
)),
),
),
);
break;
case LoginStatus.signIn:
//return People_List(signOut);
Navigator.push(context,
MaterialPageRoute(builder: (context) =>
People_List()));
break;
}
}
savePref(int value,String user, String pass) async {
SharedPreferences preferences = await SharedPreferences.getInstance();
setState(() {
preferences.setInt("value", value);
preferences.setString("user", user);
preferences.setString("pass", pass);
preferences.commit();
});
}
@override
void onLoginError(String error) {
_showSnackBar(error);
setState(() {
_isLoading = false;
});
}
@override
void onLoginSuccess(UserLoginModel user) async {
if(user != null){
savePref(1,user.emailId, user.password);
_loginStatus = LoginStatus.signIn;
}else{
_showSnackBar("Dont know");
setState(() {
_isLoading = false;
});
}
}
void navigateToRegistration(UserLoginModel userLoginModel) async{
bool result = await Navigator.push(context, MaterialPageRoute(builder: (context) {
return UserRegistration(userLoginModel);
}));
}
}
Your object UserLoginModel userLoginModel;
is not initialized, so it's still null when you write this:
firstnameController.text = userLoginModel.firstName; // userLoginModel is null so userLoginModel.firstName doesn't exist
lastnameController.text = userLoginModel.lastName;
passwordController.text = userLoginModel.password;
...
Simply initialize it: UserLoginModel userLoginModel = new UserLoginModel ();
. You'll have a concret Object, but pay attention that each attributs will be null (firstName, lastName...) until you initialize them. It's not a problem, but keep that in mind.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.