Дождитесь завершения выполнения будущей функции
I am new to flutter and in my below code there is read data function is called at line 18(it performs Firebase Database operations) I want that the readData() function must complete its execution before going to the print statement(line 19) and further execution.
Что я уже пробовал:
import 'package:firebase_auth/firebase_auth.dart'; import 'package:firebase_database/firebase_database.dart'; import 'package:flutter/cupertino.dart'; import 'package:udharibook/Screens/SignInPage.dart'; import 'package:udharibook/Screens/UserProfile.dart'; import 'package:udharibook/Screens/dashboard.dart'; class AuthService { bool flag = false; final FirebaseAuth _auth = FirebaseAuth.instance; final DBRef = FirebaseDatabase.instance.reference().child('Users'); handleAuth(){ return StreamBuilder( stream: FirebaseAuth.instance.onAuthStateChanged, builder: (BuildContext, snapshot) { if(snapshot.hasData) { readData(); print(flag); if(flag ==true) return DashboardPage(); else return UserProfile(); } else { return SignIn(); } }, ); } Future<void> readData() async { final FirebaseUser user = await _auth.currentUser(); final userid = user.uid; DBRef.child(userid).once().then((DataSnapshot data){ print(userid); if(data.value!=null) { flag = true; print(data.key); print(data.value); } else{ print('User not found'); flag = false; } }); } signOut(){ FirebaseAuth.instance.signOut(); } signIn(AuthCredential authCreds){ FirebaseAuth.instance.signInWithCredential(authCreds); } signInWithOTP(smsCode,verId){ AuthCredential authCreds = PhoneAuthProvider.getCredential( verificationId: verId, smsCode: smsCode ); signIn(authCreds); } }<