'how to authenticate in bloc based class in constructor constructor

I need to recieve the UID in the begining of the bloc as the user will use the app before signing in

  UserBloc({required this.userRepo})
      : super(UserSignedAnon())  {
    on<UserSignedAnon>(_onUserSignedAnon);
    on<UserSignedInEmail>(_onUserSignedInWithEmail);
    on<UserSignedOut>(_onUserSignedOut);
    on<UserSignedInGoogle>(_onUserSignedInGoogle);
    on<UserSignInFacebook>(_onUserSignedInFacebook);
  }

this is the Bloc constructor

class UserSignedAnon extends UserEvent {
  UserSignedAnon();

  @override
  List<Object> get props => [];
}

this is the state mentioned in the bloc

Future userSignedAnon() async {

    User? user = await _auth.signInAnon();

    return user != null ? UserModel(uid: user.uid) : null;

  }

this is the function I supposed to call in constructor but its a future.

  Future<User?> signInAnon() async {
    try {
      UserCredential result = await _auth.signInAnonymously();
      User user = result.user as User;

      return user;
    } catch (e) {
      print(e);
      return null;
    }
  }

and this is the service function I am using in this part of the code

what to change for this to work?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source