Thanks to visit codestin.com
Credit goes to github.com

Skip to content

added removeSnackBar function for removing arbitrary queued items #168253

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

heisenunes
Copy link

There is no way to remove a specific queued SnackBar once it’s been enqueued but not yet displayed (or after it has already dismissed itself). Calling .close() only works on the currently visible SnackBar; attempts to close one further back in the queue throw:

dart
final sb1 = ScaffoldMessenger.of(context).showSnackBar(...);
final sb2 = ScaffoldMessenger.of(context).showSnackBar(...);
final sb3 = ScaffoldMessenger.of(context).showSnackBar(...);
sb2.close(); // throws because sb2 is not the active SnackBar

Technical Impact:

  • Uncaught exceptions when trying to close a non-active snack bar.
  • Poor UX: Unable to cancel outdated or no-longer-relevant messages before they appear.
  • Workarounds required (try/catch, flush entire queue) that are clumsy and break user expectations.
  • Inconsistent API: removeCurrentSnackBar exists for the active item, but nothing for arbitrary queued items.

Fixes #168211

Pre-launch Checklist

If you need help, consider asking for advice on the #hackers-new channel on Discord.

Copy link

google-cla bot commented May 2, 2025

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@github-actions github-actions bot added framework flutter/packages/flutter repository. See also f: labels. f: material design flutter/packages/flutter/material repository. labels May 2, 2025
@dkwingsmt dkwingsmt requested a review from justinmc May 7, 2025 18:57
Copy link
Contributor

@justinmc justinmc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for taking a crack at this. Could you also add something to the docs of SnackbarFeatureController.close that points to your new method, so that people can find it? And actually, could you add something to the error message that is thrown when .close is called and not active?

),
);

// Trigger both snackBars
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Periods at the ends of these sentences here and below.

messenger.removeSnackBar(controller);

// It should go away immediately
await tester.pumpAndSettle();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be a single pump to show that it goes away after a single frame?

@@ -480,6 +480,23 @@ class ScaffoldMessengerState extends State<ScaffoldMessenger> with TickerProvide
hideCurrentSnackBar();
}

/// Removes the given [SnackBar] from the queue, or dismisses it
/// immediately if it’s currently visible.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add a "See also" that points to SnackbarFeatureController.close?

Copy link
Contributor

@Piinks Piinks left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the contribution!

@@ -3401,4 +3418,4 @@ class _ScaffoldScope extends InheritedWidget {
bool updateShouldNotify(_ScaffoldScope oldWidget) {
return hasDrawer != oldWidget.hasDrawer;
}
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: there should be a newline at EOF

void removeSnackBar(
ScaffoldFeatureController<SnackBar, SnackBarClosedReason> controller,
) {
// 1. If it’s queued, drop it before showing:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this assert that the snackbar is in the queue at all?

@@ -3572,4 +3656,4 @@ class _CustomPageRoute<T> extends PageRoute<T> {
) {
return child;
}
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here too

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
f: material design flutter/packages/flutter/material repository. framework flutter/packages/flutter repository. See also f: labels.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add removeSnackBar function for removing a specific snackbar from the queue
3 participants