How can I resolve a problem with firebase registration? - flutter

I'm writing an application that have to register a user with firebase and than do the login. I wrote a function for the registration but the output give me this error:
PlatformException(error, Given String is empty or null, null)
This is the function:
Future registerWithEmailAndPassword(String email, String password) async {
try {
AuthResult result = await FirebaseAuth.instance.createUserWithEmailAndPassword(email: email, password: password);
FirebaseUser user = result.user;
Navigator.push(context, MaterialPageRoute(builder: (context) => Login()));
print(user);
return user;
} catch (e) {
print(e.toString());
return null;
}
}

Related

signInWithCredential returns null without any error in flutter

I'm using Firebase to sign in with phone number it does send OTP message but when I'm trying to sign with credentials using the OTP I got and the verification ID (they're not null they do have values) it returns null here (result is null) not throwing any errors
here's my code
static Future<UserCredential?> verifyOTP(
String verificationId, String otp) async {
UserCredential? result;
try {
print(otp);
print(verificationId);
PhoneAuthCredential credential = PhoneAuthProvider.credential(
verificationId: verificationId,
smsCode: otp,
);
result = await _firebaseAuth?.signInWithCredential(credential);
User? user = _firebaseAuth?.currentUser;
print('user $user');
print('results $result');
} on FirebaseAuthException catch (error) {
print(error);
}
return result;
}
By signInWithCredential do you mean Email and Password Firebase Authentication?
If so you should use signInWithEmailAndPassword Firebase Auth method.
Example below:
try {
await _firebaseAuth.signInWithEmailAndPassword(
email: email,
password: password,
);
} catch (_) {
throw const LogInWithEmailAndPasswordFailure();
}

Flutter how to listen for email verification before user can sign in - firebase

My app allows the user to register and sends an email for verification. However i have a problem where the user can sign in with the new account whether the email is verified or not. I have looked and cannot find a solution to this bearing in mind i'm new to flutter
My Auth Code
Future<String> signIn(String email, String password) async {
AuthResult result = await _firebaseAuth.signInWithEmailAndPassword(
email: email, password: password);
FirebaseUser user = result.user;
if (user.isEmailVerified) {
return user.uid;
} else {
return null;
}
}
Future<String> signUp(String email, String password) async {
AuthResult result = await _firebaseAuth.createUserWithEmailAndPassword(
email: email, password: password);
FirebaseUser user = result.user;
try {
await user.sendEmailVerification();
return user.uid;
} catch (e) {
print("An error occurred while trying to send email verification");
print(e.message);
}
}
sign in method
try {
if (_isLoginForm) {
userId = await widget.auth.signIn(_email, _password);
print('Signed in: $userId');
} else {
userId = await widget.auth.signUp(_email, _password);
//widget.auth.sendEmailVerification();
_showVerifyEmailSentDialog();
print('Signed up user: $userId');
}
setState(() {
_isLoading = false;
});
if (userId.length > 0 && userId != null && _isLoginForm) {
widget.loginCallback();
}

Flutter, firebase. I want to show my registration form data other than the email and password and connect to a unique uid

Register Screen On Pressed method given, I believe there is a problem with calling Firebase user = result.user
onPressed: () async {
if(_formKey.currentState.validate()){
setState(() => loading = true);
dynamic result = await _auth.registerWithEmailAndPassword(email, password);
FirebaseUser user = result.user;
await DatabaseService(uid: user.uid).newUserInfo(
_nameC.text,
_cityC.text,
_contactnoC.toString()
);
if(result == null) {
setState(() {
error = 'Please supply a valid email';
loading = false;
});
}}},
// Database backend
class DatabaseService {
final String uid;
DatabaseService ({this.uid});
final CollectionReference userdata2 = Firestore.instance.collection('UserData');
Future newUserInfo(String name, String city, String contactno) async {
return await userdata2.document(uid).setData({
'name' : name,
'city' : city,
'contactno' : contactno
});
}}
// authentication backend
// register with email and password
Future registerWithEmailAndPassword(String email, String password) async {
try {
AuthResult result = await _auth.createUserWithEmailAndPassword(email: email, password: password);
FirebaseUser user = result.user;
DatabaseService(uid: user.uid);
return _userFromFirebaseUser(user);
} catch (error) {
print(error.toString());
return null;
} }
// user.dart
class User {
final String uid;
User({this.uid});
}

Cannot access function parameters from async function

In the moment of registering, when I try to print the username which is a parameter of the function, it just won't print anything on the screen and I cannot understand why.
Future registerWithEmailAndPassword(String username, String email, String password) async {
try {
AuthResult result = await _auth.createUserWithEmailAndPassword(email: email, password: password);
FirebaseUser user = result.user;
// create a new document for the user with the uid
print(username);
await DatabaseService(uid: user.uid).updateUserData(username);
return _userFromFirebaseUser(user);
} catch (error) {
print(error.toString());
return null;
}
}
Edit: this is the function which calls the registerWithEmailAndPassword function:
onPressed: () async {
if (error.length > 0)
error = "";
if (_formKey.currentState.validate()) {
setState(() => loading = true);
dynamic result = await _auth.registerWithEmailAndPassword(username, email, password);
if (result == null){
if (this.mounted){
setState(() {
error = 'Please supply a valid email';
loading = false;
});}
}
}
}

Tried to send a platform message to Flutter, but FlutterJNI was detached from native C++. Could not send. Channel: flutter/textinput. Response ID: 0

I am trying to create a user in Firebase using Flutter, but when tried to send email and password the app crashes.
and this is the code snippet:
void validateAndSubmit() async {
if (validateAndSave()) {
try {
if (_formType == FormType.login) {
AuthResult result = await FirebaseAuth.instance.createUserWithEmailAndPassword(email:_email ,
password: _password);
FirebaseUser user = result.user;
print('Login page');
print('UID is : ${user.uid}');
} else {
AuthResult result = await FirebaseAuth.instance.createUserWithEmailAndPassword(email: _email ,
password: _password);
FirebaseUser user = result.user;
print('Register Page');
print('UID is : ${user.uid}');
}
} catch (e) {
print('Error: $e');
}
}
}
You shouldn't create a new user when you're logging in!
Just call signInWithEmailAndPassword(email: _email, password: _password) which returns an AuthResult object.