1

So I have reusable snackbar on my code and I wanted to show my snackbar when user already pop from certain page. I tried to put my snackbar inside .then but It wont show anything. Any idea how to fix this ?

Here's my code:

  void openChangeClientPage(BuildContext context, user){
    Navigator.push(
      context,
      MaterialPageRoute(
        builder: (context) => AddClientPage(
          clientId: user!.uid,
          clientNickname: Provider.of<UserDataProvider>(context, listen: false).userNickname,
          clientProfileLink: Provider.of<UserDataProvider>(context, listen: false).userProfilePicture ?? '',
          ticketData: ticketData,
          ticketId: ticketId,
          source: 'ticketDetail',
        ),
      ),
    ).then((value) {
      if (value) {
        ReusableSnackBar.buildSnackBar(context, 'New Client has been assigned');
        Navigator.pop(context);
      } else {
        changeInvolvedState(true);
      }
    });
  }
Kim San
  • 575
  • 7
  • 22

3 Answers3

1

I think you can wrap the ReusableSnackBar into the Build method because snackbar tries to find the BuildContext closet but it is now in the Then method. You can read more in this

I think the code will be this:

void openChangeClientPage(BuildContext context, user){
Navigator.push(
  context,
  MaterialPageRoute(
    builder: (context) => AddClientPage(
      clientId: user!.uid,
      clientNickname: Provider.of<UserDataProvider>(context, listen: false).userNickname,
      clientProfileLink: Provider.of<UserDataProvider>(context, listen: false).userProfilePicture ?? '',
      ticketData: ticketData,
      ticketId: ticketId,
      source: 'ticketDetail',
    ),
  ),
).then((value) {
  if (value) {
    Builder(builder: (context)=>ReusableSnackBar.buildSnackBar(context, 'New Client has been assigned'));
    Navigator.pop(context);
  } else {
    changeInvolvedState(true);
  }
});}
0

The issue is that you are calling Navigator.pop immediately after calling ReusableSnackBar. This will cause the SnackBar to pop too because you can assume the snackbar is like a pop-up/dialog and is immediately dismissed.

You can call Navigator.pop before displaying the snackbar.

You can also check out this answer if you want to show snackbar without context.

Calvin Gonsalves
  • 1,738
  • 11
  • 15
0

Firstly, make sure condition ("value" variable) is true.

Remove this line in your code:

Navigator.pop(context);

This code will close current screen, not dismiss snack bar. If you want to dismiss snack bar, use:

ScaffoldMessenger.of(context).hideCurrentSnackBar();
NTA.Trong
  • 1
  • 2