Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
F
Front-End
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
abdullh.alsoleman
Front-End
Commits
0eed9add
Unverified
Commit
0eed9add
authored
Jun 30, 2022
by
Bruno Leroux
Committed by
GitHub
Jun 30, 2022
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Improve SnackBar error message when shown during build (#106658)
parent
90d6303f
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
55 additions
and
5 deletions
+55
-5
scaffold.dart
packages/flutter/lib/src/material/scaffold.dart
+38
-5
scaffold_test.dart
packages/flutter/test/material/scaffold_test.dart
+17
-0
No files found.
packages/flutter/lib/src/material/scaffold.dart
View file @
0eed9add
...
...
@@ -242,7 +242,7 @@ class ScaffoldMessengerState extends State<ScaffoldMessenger> with TickerProvide
// SNACKBAR API
/// Shows
a [SnackBar] across all registered [Scaffold]s.
/// Shows a [SnackBar] across all registered [Scaffold]s.
///
/// A scaffold can show at most one snack bar at a time. If this function is
/// called while another snack bar is already visible, the given snack bar
...
...
@@ -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
);
setState
(()
{
_snackBars
.
addLast
(
controller
);
});
_updateScaffolds
();
try
{
setState
(()
{
_snackBars
.
addLast
(
controller
);
});
_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
;
}
...
...
packages/flutter/test/material/scaffold_test.dart
View file @
0eed9add
...
...
@@ -2582,6 +2582,23 @@ void main() {
expect
(
isDrawerOpen
,
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
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment