signUp method
Registers a new user with Firebase Authentication and Firestore.
Creates a user account, stores profile data in Firestore, and saves the FCM token.
Parameters:
- email: The user's email address.
- password: The user's password.
- fullName: The user's full name.
- userType: The type of user (farmer or consumer).
- phoneNumber: The user's phone number (optional).
- address: The user's address (optional).
- farmName: The name of the user's farm (optional).
- farmLocation: The location of the user's farm (optional).
Returns: A Future<UserModel?> containing the created user model, or null if the operation fails.
Implementation
Future<UserModel?> signUp({
required String email,
required String password,
required String fullName,
required UserType userType,
String? phoneNumber,
String? address,
String? farmName,
String? farmLocation,
}) async {
try {
UserCredential result = await _auth.createUserWithEmailAndPassword(
email: email,
password: password,
);
UserModel userModel = UserModel(
uid: result.user!.uid,
email: email,
fullName: fullName,
userType: userType,
phoneNumber: phoneNumber,
address: address,
farmName: farmName,
farmLocation: farmLocation,
);
// Save user data to Firestore
await _firestore
.collection('users')
.doc(result.user!.uid)
.set(userModel.toMap());
await saveFcmToken(result.user!.uid);
return userModel;
} catch (e) {
print(e.toString());
return null;
}
}