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
345188d4
Unverified
Commit
345188d4
authored
Oct 12, 2020
by
chunhtai
Committed by
GitHub
Oct 12, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
make Router.of nullable (#67683)
parent
08576cb6
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
35 additions
and
4 deletions
+35
-4
router.dart
packages/flutter/lib/src/widgets/router.dart
+13
-4
router_test.dart
packages/flutter/test/widgets/router_test.dart
+22
-0
No files found.
packages/flutter/lib/src/widgets/router.dart
View file @
345188d4
...
...
@@ -302,10 +302,19 @@ class Router<T> extends StatefulWidget {
/// router to create a [ChildBackButtonDispatcher] for a nested router.
/// Another use case may be updating the value in [routeInformationProvider]
/// to navigate to a new route.
static
Router
<
dynamic
>
of
(
BuildContext
context
)
{
final
_RouterScope
scope
=
context
.
dependOnInheritedWidgetOfExactType
<
_RouterScope
>()!;
assert
(
scope
!=
null
);
return
scope
.
routerState
.
widget
;
static
Router
<
dynamic
>?
of
(
BuildContext
context
,
{
bool
nullOk
=
false
})
{
final
_RouterScope
?
scope
=
context
.
dependOnInheritedWidgetOfExactType
<
_RouterScope
>();
assert
(()
{
if
(
scope
==
null
&&
!
nullOk
)
{
throw
FlutterError
(
'Router operation requested with a context that does not include a Router.
\n
'
'The context used to retrieve the Router must be that of a widget that '
'is a descendant of a Router widget.'
);
}
return
true
;
}());
return
scope
?.
routerState
.
widget
;
}
/// Forces the [Router] to run the [callback] and reports the route
...
...
packages/flutter/test/widgets/router_test.dart
View file @
345188d4
...
...
@@ -79,6 +79,28 @@ void main() {
});
});
testWidgets
(
'Router.of can be null'
,
(
WidgetTester
tester
)
async
{
final
GlobalKey
key
=
GlobalKey
();
await
tester
.
pumpWidget
(
buildBoilerPlate
(
Text
(
'dummy'
,
key:
key
)
));
final
BuildContext
textContext
=
key
.
currentContext
;
// This should not throw error.
Router
<
dynamic
>
router
=
Router
.
of
(
textContext
,
nullOk:
true
);
expect
(
router
,
isNull
);
// Test when the nullOk is not specified.
bool
hasFlutterError
=
false
;
try
{
router
=
Router
.
of
(
textContext
);
}
on
FlutterError
catch
(
e
)
{
expect
(
e
.
message
.
startsWith
(
'Router'
),
isTrue
);
hasFlutterError
=
true
;
}
expect
(
hasFlutterError
,
isTrue
);
});
testWidgets
(
'Simple router can handle pop route'
,
(
WidgetTester
tester
)
async
{
final
SimpleRouteInformationProvider
provider
=
SimpleRouteInformationProvider
();
provider
.
value
=
const
RouteInformation
(
...
...
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