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
ab688b29
Commit
ab688b29
authored
Nov 01, 2016
by
Ian Hickson
Committed by
GitHub
Nov 01, 2016
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Make Scaffold.of() more helpful when it can't find a Scaffold (#6612)
parent
5ded244d
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
29 additions
and
1 deletion
+29
-1
scaffold.dart
packages/flutter/lib/src/material/scaffold.dart
+29
-1
No files found.
packages/flutter/lib/src/material/scaffold.dart
View file @
ab688b29
...
...
@@ -425,7 +425,35 @@ class Scaffold extends StatefulWidget {
/// );
/// }
/// ```
static
ScaffoldState
of
(
BuildContext
context
)
=>
context
.
ancestorStateOfType
(
const
TypeMatcher
<
ScaffoldState
>());
///
/// If there is no [Scaffold] in scope, then this will throw an exception.
/// To return null if there is no [Scaffold], then pass `nullOk: true`.
static
ScaffoldState
of
(
BuildContext
context
,
{
bool
nullOk:
false
})
{
assert
(
nullOk
!=
null
);
assert
(
context
!=
null
);
ScaffoldState
result
=
context
.
ancestorStateOfType
(
const
TypeMatcher
<
ScaffoldState
>());
if
(
nullOk
||
result
!=
null
)
return
result
;
throw
new
FlutterError
(
'Scaffold.of() called with a context that does not contain a Scaffold.
\n
'
'No Scaffold could be found starting from the context that was passed to Scaffold.of(). '
'This usually happens when the context provided is from the same StatefulWidget as that '
'whose build function actually creates the Scaffold widget being sought.
\n
'
'There are several ways to avoid this problem. The simplest is to use a Builder to get a '
'context that is "under" the Scaffold. For an example of this, please see the '
'documentation for Scaffold.of():
\n
'
' https://docs.flutter.io/flutter/material/Scaffold/of.html
\n
'
'A more efficient solution is to split your build function into several widgets. This '
'introduces a new context from which you can obtain the Scaffold. In this solution, '
'you would have an outer widget that creates the Scaffold populated by instances of '
'your new inner widgets, and then in these inner widgets you would use Scaffold.of().
\n
'
'A less elegant but more expedient solution is assign a GlobalKey to the Scaffold, '
'then use the key.currentState property to obtain the ScaffoldState rather than '
'using the Scaffold.of() function.
\n
'
'The context used was:
\n
'
'
$context
'
);
}
@override
ScaffoldState
createState
()
=>
new
ScaffoldState
();
...
...
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