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
02cfbc32
Unverified
Commit
02cfbc32
authored
Nov 10, 2021
by
Simon Binder
Committed by
GitHub
Nov 10, 2021
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Improve error message for uninitialized channel (#92758)
parent
d81d4d3b
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
35 additions
and
1 deletion
+35
-1
platform_channel.dart
packages/flutter/lib/src/services/platform_channel.dart
+11
-1
use_platform_channel_without_initialization_test.dart
...ces/use_platform_channel_without_initialization_test.dart
+24
-0
No files found.
packages/flutter/lib/src/services/platform_channel.dart
View file @
02cfbc32
...
...
@@ -129,7 +129,17 @@ class MethodChannel {
/// The messenger used by this channel to send platform messages.
///
/// The messenger may not be null.
BinaryMessenger
get
binaryMessenger
=>
_binaryMessenger
??
ServicesBinding
.
instance
!.
defaultBinaryMessenger
;
BinaryMessenger
get
binaryMessenger
{
assert
(
_binaryMessenger
!=
null
||
ServicesBinding
.
instance
!=
null
,
'Cannot use this MethodChannel before the binary messenger has been initialized. '
'This happens when you invoke platform methods 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().'
,
);
return
_binaryMessenger
??
ServicesBinding
.
instance
!.
defaultBinaryMessenger
;
}
final
BinaryMessenger
?
_binaryMessenger
;
/// Backend implementation of [invokeMethod].
...
...
packages/flutter/test/services/use_platform_channel_without_initialization_test.dart
0 → 100644
View file @
02cfbc32
// 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'
);
// Ensure that accessing the binary messenger before initialization reports
// a helpful error message.
expect
(()
=>
methodChannel
.
binaryMessenger
,
throwsA
(
isA
<
AssertionError
>()
.
having
((
AssertionError
e
)
=>
e
.
message
,
'message'
,
contains
(
'WidgetsFlutterBinding.ensureInitialized()'
))));
TestWidgetsFlutterBinding
.
ensureInitialized
();
expect
(()
=>
methodChannel
.
binaryMessenger
,
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