getCurrentUser static method

User? getCurrentUser(
  1. BuildContext context, {
  2. dynamic customMapping(
    1. User?
    )?,
})

An easy way to get the Currently Logged in User

context is necesssary customMapping is an optional Function accepting a User as its arguement and returns it in a format of your choice, for example creating a custom User Model.

The recieved User will have all its data filled for GoogleSignIn only, for other forms of Auth Some of the User Data for example email, displayName etc will be empty.

emailVerified is true only for GoogleSignIn

Example:

 AuthController.getCurrentUser(
  context,
  customMapping: (user) => {
    'name': user.displayName,
    'email': user.email,
  },
);

Implementation

static User? getCurrentUser(
  BuildContext context, {
  Function(User?)? customMapping,
}) {
  final provider = Provider.of<FireAuthProvider>(context, listen: false);
  User? cUser = provider.authInstance.currentUser;
  if (customMapping != null) return customMapping(cUser);
  return cUser;
}