@@ -242,7 +242,7 @@ class ScaffoldMessengerState extends State<ScaffoldMessenger> with TickerProvide
242242
243243 // SNACKBAR API
244244
245- /// Shows a [SnackBar] across all registered [Scaffold] s.
245+ /// Shows a [SnackBar] across all registered [Scaffold] s.
246246 ///
247247 /// A scaffold can show at most one snack bar at a time. If this function is
248248 /// called while another snack bar is already visible, the given snack bar
@@ -289,10 +289,43 @@ class ScaffoldMessengerState extends State<ScaffoldMessenger> with TickerProvide
289289 },
290290 null , // SnackBar doesn't use a builder function so setState() wouldn't rebuild it
291291 );
292- setState (() {
293- _snackBars.addLast (controller);
294- });
295- _updateScaffolds ();
292+ try {
293+ setState (() {
294+ _snackBars.addLast (controller);
295+ });
296+ _updateScaffolds ();
297+ } catch (exception) {
298+ assert (() {
299+ if (exception is FlutterError ) {
300+ final String summary = exception.diagnostics.first.toDescription ();
301+ if (summary == 'setState() or markNeedsBuild() called during build.' ) {
302+ final List <DiagnosticsNode > information = < DiagnosticsNode > [
303+ ErrorSummary ('The showSnackBar() method cannot be called during build.' ),
304+ ErrorDescription (
305+ 'The showSnackBar() method was called during build, which is '
306+ 'prohibited as showing snack bars requires updating state. Updating '
307+ 'state is not possible during build.' ,
308+ ),
309+ ErrorHint (
310+ 'Instead of calling showSnackBar() during build, call it directly '
311+ 'in your on tap (and related) callbacks. If you need to immediately '
312+ 'show a snack bar, make the call in initState() or '
313+ 'didChangeDependencies() instead. Otherwise, you can also schedule a '
314+ 'post-frame callback using SchedulerBinding.addPostFrameCallback to '
315+ 'show the snack bar after the current frame.' ,
316+ ),
317+ context.describeOwnershipChain (
318+ 'The ownership chain for the particular ScaffoldMessenger is' ,
319+ ),
320+ ];
321+ throw FlutterError .fromParts (information);
322+ }
323+ }
324+ return true ;
325+ }());
326+ rethrow ;
327+ }
328+
296329 return controller;
297330 }
298331
0 commit comments