wait for an action to complete and do another in dart

367 views
Skip to first unread message

ABEROR NORBERT

unread,
Jul 22, 2018, 8:28:24 PM7/22/18
to Flutter Dev
I have written a function that I use to sign in user with email and password. I want to push the user to two diffirent pages with a button press if the firebase user uid returns null or not.I think the problem is that by the time the page is pushed the user uid is always null. I want to wait until the signInWithEmailAndPassword() method completes. what should I do
 something like:
 firstFunction(
//this communicates with firebase authentication and returns the uid
)
secondFunction(
if(uid){ /*do something*/}
else{}
)
//buh I want `firstFunction()` to finish executing and give me back results before `secondFunction() `executes. so the second should execute only //after the first has executed
here is my code
Future<FirebaseUser> signInUser([var myuserdata,var mypasswrd
,frouter,srouter]
)async{

FirebaseUser firebaseUser = await FirebaseAuth.instance
.signInWithEmailAndPassword(email:myuserdata,password:mypasswrd).then((onValue){
if(onValue.uid != null){

print('object at non-null uid ${onValue.uid}');
frouter;
//frouter is my homepage;;
}
if(onValue.uid == null){
srouter;
print('object at null uid ${onValue.uid}');
//srouter is my error page;
}
});
print('user id :${firebaseUser.uid}');
return firebaseUser;

}
// it always push me to the error page //how do I make this work

bre...@tamu.edu

unread,
Jul 23, 2018, 9:35:31 AM7/23/18
to Flutter Dev
You'll want to look into the Future API for handling things like this. As a very basic example:


Future getUIDFromDB = _db.getUID();

getUIDFromDB
.then((returnData) {
   
if (returnData != null) {
       
// push to one screen
   
}
   
else {
       
// push to another screen
   
}
});
Reply all
Reply to author
Forward
0 new messages