I'm using a Google Auth Sign in after the user signs in a Method initalize();
runs and states wether the user is a new user or existed user. but its always detecting a user as new user and resetting the data to default.
Google Login :
Future googleLogin() async { try { final googleUser = await googleSignIn.signIn(); if (googleUser == null) return; _user = googleUser; final googleAuth = await googleUser.authentication; final credential = GoogleAuthProvider.credential( accessToken: googleAuth.accessToken, idToken: googleAuth.idToken, ); await FirebaseAuth.instance.signInWithCredential(credential); initalize(); //<--------------The problem } catch (e) { print(e.toString()); } notifyListeners(); }
Initialize() method:
initalize() async {
var user = FirebaseAuth.instance.currentUser!;
final snapShot = await FirebaseFirestore.instance
.collection('expenses')
.doc(user.uid)
.get();
if (!snapShot.exists) {
print(snapShot);
await SetTransaction(
uid: user.uid,
t_name: 't_name',
t_amt: '0',
isIncome: true,
Category: 'Any');
}
else{print('Yesss siirrr');}
}
Extra code
The google login happens here:
return Scaffold(
resizeToAvoidBottomInset:false,
backgroundColor: const Color(0xffedf1f4),
body: StreamBuilder(
stream:FirebaseAuth.instance.authStateChanges(),
builder: (context, snapshot){
if(snapshot.connectionState == ConnectionState.waiting) {
return Center(child: CircularProgressIndicator());
} else if (snapshot.hasData){
return LoggedInWidget();
}
else if (snapshot.hasError) {
return Center(child: Text('Something Went Wrong!'));
}else {
return SignUpWidget();
}
},
),
);
Button inside SignUpWidget();
class which helps to login:
GestureDetector(
onTap: () { //<------this happens on click of the button
print("Clicked");
final provider = Provider.of<GoogleSignInProvider>(
context,
listen: false);
provider.googleLogin();
},
child: Container(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
FaIcon(FontAwesomeIcons.google),
Text("Sign Up with Google")
],
),
padding: EdgeInsets.only(
left: 20, right: 20, top: 15, bottom: 15),
margin: EdgeInsets.symmetric(horizontal: 70.0),
decoration: BoxDecoration(
color: Color(0xffedf1f4),
borderRadius: BorderRadius.circular(12),
boxShadow: [
BoxShadow(
color: Colors.grey.shade600,
offset: Offset(5, 5),
blurRadius: 5,
spreadRadius: 1),
BoxShadow(
color: Colors.white,
offset: Offset(-5, -5),
blurRadius: 5,
spreadRadius: 1,
)
],
),
),
),
CodePudding user response:
googleLogin
is a future method, it needs to await
GestureDetector(
onTap: () async{
...
await provider.googleLogin();
},
More about sync-await.
CodePudding user response:
you will follow this and this, get some idea about 'snapShot.exists' and I hope you will found your solution.