I get this error while fetching the teacher id, what can be done??
CodePudding user response:
That's because you are using Future Function to get the current user, you can get the current user simply like this:
User teacher = FirebaseAuth.instance.currentUser!;
CodePudding user response:
That happens because you are trying to access a future result when it's not ready yet.
As you see, your teacher
variable is a Future<FirebaseUser>
, so at that point moving fast machine speed you reach the access to teacher.uid
before it's completed and still a promise.
On that line that you call the teacher, change from:
Future<FirebaseUser> Function() teacher = FirebaseAuth.instance.currentUser;
To this:
FirebaseUser teacher = FirebaseAuth.instance.currentUser!;
With this you code will wait until FirebaseAuth do all requests and return only the result.
You can look an example at https://github.com/FirebaseExtended/flutterfire/blob/8a2f512e6d52fa8e47ffc53e55b2593de61e2085/packages/firebase_auth/firebase_auth/example/lib/profile.dart#L34 Also at https://firebase.flutter.dev/docs/auth/usage