Flutter sign up not storing data in firebase NO ERR - flutter

I don't know what's wrong but no error is coming out but I can't see the details store in my firebase authentication, it is also not storing in my buyers collection and in the storage.. Thank you.
I dont know what seems to be the problem because it always says success and nothing comes out in the terminal.
class AuthController {
//to store users to firebase 'Authentication'
final FirebaseAuth _auth = FirebaseAuth.instance;
//to store users to cloud firestore
final FirebaseFirestore _firestore = FirebaseFirestore.instance;
final FirebaseStorage _storage = FirebaseStorage.instance;
var image;
_uploadProfileImageToStorage(Uint8List? image) async {
Reference ref =
_storage.ref().child('profilePics').child(_auth.currentUser!.uid);
UploadTask uploadTask = ref.putData(image!);
TaskSnapshot snapshot = await uploadTask;
String downloadUrl = await snapshot.ref.getDownloadURL();
return downloadUrl;
}
pickProfileImage(ImageSource source) async {
final ImagePicker _imagePicker = ImagePicker();
XFile? _file = await _imagePicker.pickImage(source: ImageSource.gallery);
if (_file != null) {
return await _file.readAsBytes();
} else {
print('No Image Selected');
}
}
Future<String> signupUsers(String email, String fullName, String phoneNumber,
String password) async {
String res = 'Some errors occurred';
try {
if (email.isNotEmpty &&
fullName.isNotEmpty &&
phoneNumber.isNotEmpty &&
password.isNotEmpty &&
image != null) {
//Create the users
UserCredential cred = await _auth.createUserWithEmailAndPassword(
email: email, password: password);
String profileImageUrl = await _uploadProfileImageToStorage(image);
await _firestore.collection('buyers').doc(cred.user!.uid).set({
'email': email,
'fullName': fullName,
'phoneNumber': phoneNumber,
'buyerId': cred.user!.uid,
//'address': '',
'profileImage': profileImageUrl,
});
res = 'success';
} else {
res = 'Please Fields must not be empty';
}
} catch (e) {}
return res;
}
//login users
loginUsers(String email, String password) async {
String res = 'Something went wrong.';
try {
if (email.isNotEmpty && password.isNotEmpty) {
await _auth.signInWithEmailAndPassword(
email: email, password: password);
res = 'success';
} else {
res = 'Fields must not be empty';
}
} catch (e) {
res = e.toString();
}
return res;
}
}
I have tried everything but nothing seems to wor. No error too

Related

How can i store the url of the image instead of path of the image in firestore flutter

I want to store the URL of the image like this -
this is how i want to store url of the image in firestore
This is my current condition -
Currently it's showing the path of the image not the url
onPressed: () async {
if (imageFile == null) {
print("error");
} else {
try {
final ref = storage
.ref()
.child('user_profile_images')
.child(name + '.jpg');
ref.putFile(imageFile!);
url = await ref.getDownloadURL();
if (url != null) {
print(url);
}
} catch (e) {
print(e);
}
dynamic user = await auth.createUserWithEmailAndPassword(
email: email, password: password);
await FirebaseFirestore.instance
.collection('users')
.doc(name)
.set({
'email': email,
'name': name,
'profileurl': imageFile?.path,
'userid': auth.currentUser?.uid
}).then((value) {
try {
if (user != null) {
Navigator.pushNamed(context, '/dashboard');
}
} catch (e) {
print(e);
}
});
}
}),
I have a feeling the multiple try/catch blocks are getting in your way. In addition, it is never a good idea to mix then and await in a single block of code.
Combining those, try the following:
try {
final ref = storage
.ref()
.child('user_profile_images')
.child(name + '.jpg');
ref.putFile(imageFile!);
url = await ref.getDownloadURL();
if (url != null) {
print(url);
}
dynamic user = await auth.createUserWithEmailAndPassword(
email: email, password: password);
dynamic value = await FirebaseFirestore.instance
.collection('users')
.doc(name)
.set({
'email': email,
'name': name,
'profileurl': url // 👈
'userid': auth.currentUser?.uid
});
if (user != null) {
Navigator.pushNamed(context, '/dashboard');
}
} catch (e) {
print(e);
}
'profileurl': imageFile?.path, => 'profileurl': url?.toString(),

Async not waiting for complete

I'm trying to make the following method wait for complete but it stops before the line "print(user)":
Future<ModelUser> createUserWithEmailAndPassword({
#required String email,
#required String password,
}) async {
assert(email != null);
assert(password != null);
var user = new ModelUser(email: email, password: password);
final response = await post(Uri.http(_options.baseUrl, '/createAccount'),
headers: _headers, body: jsonEncode(user.toJson()));
if (response.statusCode == 200) {
Map<String, dynamic> userMap = jsonDecode(response.body);
var user = ModelUser.fromJson(userMap);
print(user);
return ModelUser.fromJson(jsonDecode(response.body));
}
return null;
}
I'm calling it from another method, using await:
Future<ModelUser> registerWithEmailAndPassword(
String email, String password) async {
try {
return _user(await _auth.createUserWithEmailAndPassword(
email: email, password: password));
} catch (e) {
return null;
}
}

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;
});}
}
}
}