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
38186c89
Unverified
Commit
38186c89
authored
Aug 25, 2021
by
creativecreatorormaybenot
Committed by
GitHub
Aug 25, 2021
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add WidgetsFlutterBinding Assertion to setMethodCallHandler (#88819)
parent
fad666bd
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
33 additions
and
0 deletions
+33
-0
platform_channel.dart
packages/flutter/lib/src/services/platform_channel.dart
+7
-0
set_method_call_handler_test.dart
...s/flutter/test/services/set_method_call_handler_test.dart
+26
-0
No files found.
packages/flutter/lib/src/services/platform_channel.dart
View file @
38186c89
...
...
@@ -372,6 +372,13 @@ class MethodChannel {
/// similarly to what happens if no method call handler has been set.
/// Any other exception results in an error envelope being sent.
void
setMethodCallHandler
(
Future
<
dynamic
>
Function
(
MethodCall
call
)?
handler
)
{
assert
(
_binaryMessenger
!=
null
||
ServicesBinding
.
instance
!=
null
,
'Cannot set the method call handler before the binary messenger has been initialized. '
'This happens when you call setMethodCallHandler() before the WidgetsFlutterBinding '
'has been initialized. You can fix this by either calling WidgetsFlutterBinding.ensureInitialized() '
'before this or by passing a custom BinaryMessenger instance to MethodChannel().'
,
);
binaryMessenger
.
setMessageHandler
(
name
,
handler
==
null
...
...
packages/flutter/test/services/set_method_call_handler_test.dart
0 → 100644
View file @
38186c89
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import
'package:flutter/services.dart'
;
import
'package:flutter_test/flutter_test.dart'
;
void
main
(
)
{
// We need a separate test file for this test case (instead of including it
// in platform_channel_test.dart) since we rely on the WidgetsFlutterBinding
// not being initialized and we call ensureInitialized() in the other test
// file.
test
(
'throws assertion error iff WidgetsFlutterBinding is not yet initialized'
,
()
{
const
MethodChannel
methodChannel
=
MethodChannel
(
'mock'
);
// Verify an assertion error is thrown before the binary messenger is
// accessed (which would result in a _CastError due to the non-null
// assertion). This way we can hint the caller towards how to fix the error.
expect
(()
=>
methodChannel
.
setMethodCallHandler
(
null
),
throwsAssertionError
);
// Verify the assertion is not thrown once the binding has been initialized.
// This cannot be a separate test case since the execution order is random.
TestWidgetsFlutterBinding
.
ensureInitialized
();
expect
(()
=>
methodChannel
.
setMethodCallHandler
(
null
),
returnsNormally
);
});
}
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