I am creating a sign up and sign in with email and password funcion using firebase. I have a user model in which I add or update to the firestore when user has not been registered or sign in. but I get an error from the function saying The argument type ‘String’ can’t be assigned to the parameter type ‘int’.
This is my User Model:
import 'package:cloud_firestore/cloud_firestore.dart';
class UserModel {
late final int uid;
late final String email;
late final String displayName;
late final String photoUrl;
late final bool isAdmin;
late final bool isGoogleUser;
UserModel(
{required this.uid,
required this.email,
required this.displayName,
required this.photoUrl,
this.isAdmin = false,
this.isGoogleUser = false});
factory UserModel.fromFirestore(DocumentSnapshot snapshot) {
return UserModel(
uid: snapshot['uid'],
email: snapshot['email'],
displayName: snapshot['displayName'],
photoUrl: snapshot['photoUrl'],
isAdmin: snapshot['isAdmin'] ?? false,
isGoogleUser: snapshot['isGoogleUser'] ?? false,
);
}
}
This is the register with email and password function as well as the sign in with email and password function;
//Registering a new user unto firebase....................................................
Future<AuthStatus> registerWithEmailAndPassword(
String username, String email, String password) async {
try {
final UserCredential userCredential = await _auth
.createUserWithEmailAndPassword(email: email, password: password);
if (userCredential.user != null) {
UserModel userModel = UserModel(
uid: userCredential.user!.uid, //error: The argument type 'String' can't be assigned to the parameter type 'int'.
email: email,
displayName: username,
photoUrl: '');
// Add the UserModel data to a Firestore collection
await _usersRef.doc(userModel.uid).set({ //error: The argument type 'int' can't be assigned to the parameter type 'String?'.
'uid': userModel.uid,
'email': userModel.email,
'username': userModel.displayName,
'photoUrl': userModel.photoUrl,
});
// Return a successful AuthStatus
return AuthStatus(
isSuccessful: true,
errorMessage: '',
userModel: userModel,
);
} else {
// If userCredential.user is null, registration failed
return AuthStatus(
isSuccessful: false,
errorMessage: 'User registration failed',
userModel:
UserModel(uid: '', email: '', displayName: '', photoUrl: ''),//error: The argument type 'String' can't be assigned to the parameter type 'int'.
);
}
} catch (e) {
// Handle any errors during registration
return AuthStatus(
isSuccessful: false,
errorMessage: e.toString(),
userModel: UserModel(uid: '', email: '', displayName: '', photoUrl: ''),//error: The argument type 'String' can't be assigned to the parameter type 'int'.
);
}
}
//SignIn a registered member or user to firebase.........................................
Future<UserModel?> signInWithEmailAndPassword(
String username, String password) async {
try {
final UserCredential userCredential =
await _auth.signInWithEmailAndPassword(
email: username,
password: password,
);
if (userCredential.user != null) {
UserModel userModel = UserModel(
uid: userCredential.user!.uid, //error: The argument type 'String' can't be assigned to the parameter type 'int'.
email: userCredential.user!.email ?? '',
displayName: userCredential.user!.displayName ?? '',
photoUrl: '',
);
return userModel;
} else {
return null;
}
} catch (e) {
print('Sign-in error: $e');
return null;
}
}
This is the AuthStatus class:
import 'package:kp_app/models/user_model.dart';
class AuthStatus {
final bool isSuccessful;
final String errorMessage;
final UserModel userModel;
AuthStatus({
required this.isSuccessful,
required this.errorMessage,
required this.userModel,
});
}
2
Answers
uid in UserModel should be of type string instead of int. Change the data type.
userCredential.user!.uid
is a string, and at theUserModel
it’s declared as anint
Try