show method

StreamSnackbarController show(
  1. StreamSnackbar snackbar, {
  2. bool replace = false,
})

Enqueues snackbar for display.

Returns a StreamSnackbarController. If another snackbar is currently shown, controller.closed does not resolve until the queue advances to this snackbar and it is then dismissed.

When replace is true, the currently-displayed snackbar (if any) is removed without an exit animation before enqueueing — equivalent to calling removeCurrent first. Useful for "snap to the freshest" UX (e.g. a hint that should reset on each retrigger).

Implementation

StreamSnackbarController show(
  StreamSnackbar snackbar, {
  bool replace = false,
}) {
  assert(!_disposed, 'show called after dispose');
  if (replace) removeCurrent();
  final hosted = _HostedSnackbar(snackbar: snackbar, host: this);
  _queue.add(hosted);
  if (_queue.length != 1) return hosted.controller;
  try {
    notifyListeners();
  } catch (exception) {
    assert(() {
      if (exception is FlutterError) {
        final summary = exception.diagnostics.first.toDescription();
        if (summary == 'setState() or markNeedsBuild() called during build.') {
          final information = <DiagnosticsNode>[
            ErrorSummary('The show() method cannot be called during build.'),
            ErrorDescription(
              'The show() method was called during build, which is '
              'prohibited as showing snack bars requires updating state. Updating '
              'state is not possible during build.',
            ),
            ErrorHint(
              'Instead of calling show() during build, call it directly '
              'in your on tap (and related) callbacks. If you need to immediately '
              'show a snack bar, make the call in initState() or '
              'didChangeDependencies() instead. Otherwise, you can also schedule a '
              'post-frame callback using SchedulerBinding.addPostFrameCallback to '
              'show the snack bar after the current frame.',
            ),
          ];
          throw FlutterError.fromParts(information);
        }
      }
      return true;
    }());
    rethrow;
  }
  return hosted.controller;
}