StreamBuilder for Amplify Auth State on flutter?

131 views Asked by At

Trying to update BottomNavigationBarItems based on user successful signUp/signIn. Not an expert in flutter or AWS, but have tried using either of the following to update a bool flag to render _listA or _listB

  • Amplify.Auth.fetchAuthSession()
  • Amplify.Auth.getCurrentUser()

The problem I'm having is the BottomNavigationBar renders faster than the above functions and app needs to reload to show the correct bar items.

Is there a solution like the following?

bottomNavigationBar: StreamBuilder<SomeState>(
   stream: Amplify.Auth.SomeState,
   builder: (context, snapshot) {
     return BottomNavigationBar(
...

Any other alternative would be appreciated as well.

1

There are 1 answers

0
Ravan Alaskarov On

You can't directly use a StreamBuilder for this job, but you can use StreamsubScription for auth events:

final subscription = Amplify.Hub.listen(HubChannel.Auth,(AuthHubEvent event) {
  switch (event.type) {
    case AuthHubEventType.signedIn:
      safePrint('User is signed in.');
      break;
    case AuthHubEventType.signedOut:
      safePrint('User is signed out.');
      break;
    case AuthHubEventType.sessionExpired:
      safePrint('The session has expired.');
      break;
    case AuthHubEventType.userDeleted:
      safePrint('The user has been deleted.');
      break;
  }
});

You can read the documentation for more information: https://docs.amplify.aws/lib/auth/auth-events/q/platform/flutter/