Unverified Commit 0eed9add authored by Bruno Leroux's avatar Bruno Leroux Committed by GitHub

Improve SnackBar error message when shown during build (#106658)

parent 90d6303f
...@@ -289,10 +289,43 @@ class ScaffoldMessengerState extends State<ScaffoldMessenger> with TickerProvide ...@@ -289,10 +289,43 @@ class ScaffoldMessengerState extends State<ScaffoldMessenger> with TickerProvide
}, },
null, // SnackBar doesn't use a builder function so setState() wouldn't rebuild it null, // SnackBar doesn't use a builder function so setState() wouldn't rebuild it
); );
try {
setState(() { setState(() {
_snackBars.addLast(controller); _snackBars.addLast(controller);
}); });
_updateScaffolds(); _updateScaffolds();
} catch (exception) {
assert (() {
if (exception is FlutterError) {
final String summary = exception.diagnostics.first.toDescription();
if (summary == 'setState() or markNeedsBuild() called during build.') {
final List<DiagnosticsNode> information = <DiagnosticsNode>[
ErrorSummary('The showSnackBar() method cannot be called during build.'),
ErrorDescription(
'The showSnackBar() 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 showSnackBar() 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.',
),
context.describeOwnershipChain(
'The ownership chain for the particular ScaffoldMessenger is',
),
];
throw FlutterError.fromParts(information);
}
}
return true;
}());
rethrow;
}
return controller; return controller;
} }
......
...@@ -2582,6 +2582,23 @@ void main() { ...@@ -2582,6 +2582,23 @@ void main() {
expect(isDrawerOpen, false); expect(isDrawerOpen, false);
expect(isEndDrawerOpen, false); expect(isEndDrawerOpen, false);
}); });
testWidgets('ScaffoldMessenger showSnackBar throws an intuitive error message if called during build', (WidgetTester tester) async {
await tester.pumpWidget(MaterialApp(
home: Scaffold(
body: Builder(
builder: (BuildContext context) {
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text('SnackBar')));
return const SizedBox.shrink();
},
),
),
));
final FlutterError error = tester.takeException() as FlutterError;
final ErrorSummary summary = error.diagnostics.first as ErrorSummary;
expect(summary.toString(), 'The showSnackBar() method cannot be called during build.');
});
} }
class _GeometryListener extends StatefulWidget { class _GeometryListener extends StatefulWidget {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment