I am trying to use the flutter firebase Signup method in my application but it is not working as expected, The value of Textformfields is null according to the app while there is still text written
i am also trying to save data to firestore by attaching a model
here is my model code
class UserModel {
String name;
String userName;
String email;
String password;
UserModel(
{required this.name,
required this.userName,
required this.email,
required this.password});
Map<String, dynamic> toMap() {
return {
'FullName': name,
'userName': userName,
'userEmail': email,
'Password': password,
};
}
factory UserModel.fromMap(Map<String, dynamic> map) {
return UserModel(
name: map['name'],
userName: map['userName'],
email: map['email'],
password: map['password']);
}
}
here is my firebase signup method, everything seems fine according to me
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:zoom_clone/models/user_mode.dart';
import 'package:zoom_clone/utils/snackbar.dart';
class FirebaseSignUpService extends ChangeNotifier {
final FirebaseAuth _auth = FirebaseAuth.instance;
final FirebaseFirestore _firestore = FirebaseFirestore.instance;
Future<void> signUpUser({
required String name,
required String userName,
required String email,
required String password,
}) async {
if (name.isEmpty || userName.isEmpty || email.isEmpty || password.isEmpty) {
Snackbar(snackBartext: "All fields are required", isError: true);
return;
}
try {
UserCredential userCredential =
await _auth.createUserWithEmailAndPassword(
email: email,
password: password,
);
UserModel userModel = UserModel(
name: name,
userName: userName,
email: email,
password: password,
);
await _firestore
.collection('users')
.doc(userCredential.user?.uid)
.set(userModel.toMap());
notifyListeners();
Snackbar(snackBartext: 'Account Created Successfully', isError: false);
} on FirebaseException catch (e) {
Snackbar(snackBartext: e.message.toString(), isError: true);
}
}
}
I am using form and TextForm validator to check if the values are not empty or null
below is the code to my UI
import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:zoom_clone/services/signup_services.dart';
import 'package:zoom_clone/utils/primarybtn.dart';
import 'package:zoom_clone/views/components/txtformfield.dart';
class Signupui extends StatefulWidget {
const Signupui({super.key});
@override
State<Signupui> createState() => _SignupuiState();
}
class _SignupuiState extends State<Signupui> {
TextEditingController nameController = TextEditingController();
TextEditingController usernameController = TextEditingController();
TextEditingController emailController = TextEditingController();
TextEditingController passwordController = TextEditingController();
TextEditingController confirmController = TextEditingController();
FirebaseSignUpService signUpService = FirebaseSignUpService();
final formKey = GlobalKey<FormState>();
@override
Widget build(BuildContext context) {
Size size = MediaQuery.of(context).size;
return Scaffold(
appBar: AppBar(
automaticallyImplyLeading: true,
backgroundColor: Colors.transparent,
elevation: 0,
scrolledUnderElevation: 0,
),
body: Padding(
padding: EdgeInsets.symmetric(
horizontal: size.width * 0.07,
),
child: Center(
child: SingleChildScrollView(
padding: EdgeInsets.symmetric(vertical: size.height * 0.15),
keyboardDismissBehavior: ScrollViewKeyboardDismissBehavior.onDrag,
child: Form(
key: formKey,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Align(
alignment: Alignment.centerLeft,
child: Text(
'Create An Account',
style: TextStyle(fontSize: 22),
),
),
SizedBox(
height: size.height * 0.02,
),
Txtformfield(
validator: (value) {
if (value == null || value.isEmpty) {
return 'Enter Your Name';
}
return null;
},
labelText: 'Enter Your Name',
isPassword: false,
icon: FontAwesomeIcons.user,
controller: nameController,
),
SizedBox(
height: size.height * 0.02,
),
Txtformfield(
validator: (value) {
if (value == null || value.isEmpty) {
return 'Enter Your User Name';
}
return null;
},
labelText: 'Username',
isPassword: false,
icon: FontAwesomeIcons.a,
controller: usernameController,
),
SizedBox(
height: size.height * 0.02,
),
Txtformfield(
validator: (value) {
if (value == null || value.isEmpty) {
return 'Email is Required';
}
return null;
},
labelText: 'Enter Your Email',
isPassword: false,
icon: FontAwesomeIcons.at,
controller: emailController,
),
SizedBox(
height: size.height * 0.02,
),
Txtformfield(
validator: (value) {
if (value == null || value.isEmpty) {
return 'Password cannot be empty';
}
return null;
},
labelText: 'Password',
isPassword: true,
icon: FontAwesomeIcons.key,
controller: passwordController,
),
SizedBox(
height: size.height * 0.02,
),
Txtformfield(
validator: (value) {
if (value == null || value.isEmpty) {
return 'Confirm Your Password';
}
if (value != passwordController.text) {
return 'Passwords do not match';
}
return null;
},
labelText: 'Confirm Password',
isPassword: true,
icon: FontAwesomeIcons.key,
controller: confirmController,
),
SizedBox(
height: size.height * 0.03,
),
Primarybtn(
btnText: 'SIGN UP!',
onTap: () {
if (formKey.currentState?.validate() ?? false) {
signUpService.signUpUser(
name: nameController.text,
userName: usernameController.text,
email: emailController.text,
password: passwordController.text,
);
} else {
print('Please enter all values');
}
},
width: size.width * 0.6,
height: size.height * 0.07,
radius: 20,
size: size.width * 0.055,
),
],
),
),
),
),
),
);
}
}
Instead of the function call in the if block the print statement gets printed in the console