I have an issue with utilising the current user's id (UID). The following code 'works' however there are instances where the _currentUID first outputs a 'null' before outputting the correct value.
class _ContactsScreenState extends State<ContactsScreen> {
String _currentUID;
@override
initState() {
super.initState();
loadCurrentUser();
}
loadCurrentUser() async {
var currentUID = await _getCurrentUID();
setState(() {
this._currentUID = currentUID;
});
}
Future<String> _getCurrentUID() async {
FirebaseUser user = await FirebaseAuth.instance.currentUser();
return user.uid;
}
@override
Widget build(BuildContext context) {
if (_currentUID == null){
print("current UserID = null");
} else {
print("current UserID = $_currentUID");
}
return StreamBuilder(
...
So this is actually working fine, outputs the results as expected, however upon inspection the printed output is as follows:
flutter: current UserID = null // why is it printing null?
flutter: current UserID = abcd1234abcd //correct
What is unusual is that this will only occur when the user visit's the screen for the 2nd time. The first time the screen/page loads it will correctly output 'only' the actual Current User ID. It when the user goes back to the same page it then will print current user twice (as shown above).
AuthStateListener.currentUser()function is not async.