Skip to content

Instantly share code, notes, and snippets.

@ened
Last active July 2, 2020 14:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ened/ffa099b7e5468287b841ec6290ea8d1f to your computer and use it in GitHub Desktop.
Save ened/ffa099b7e5468287b841ec6290ea8d1f to your computer and use it in GitHub Desktop.
diff --git a/packages/flutter/lib/src/foundation/binding.dart b/packages/flutter/lib/src/foundation/binding.dart
index 957f85906..779f320a5 100644
--- a/packages/flutter/lib/src/foundation/binding.dart
+++ b/packages/flutter/lib/src/foundation/binding.dart
@@ -50,16 +50,24 @@ abstract class BindingBase {
/// instance pointers and other state, then calls
/// [initServiceExtensions] to have bindings initialize their
/// observatory service extensions, if any.
- BindingBase() {
+ BindingBase({bool background = false}) {
developer.Timeline.startSync('Framework initialization');
- assert(!_debugInitialized);
- initInstances();
- assert(_debugInitialized);
+ if (background != null && background) {
+ initBackgroundInstances();
- assert(!_debugServiceExtensionsRegistered);
- initServiceExtensions();
- assert(_debugServiceExtensionsRegistered);
+ // assert(!_debugServiceExtensionsRegistered);
+ // initServiceExtensions();
+ // assert(_debugServiceExtensionsRegistered);
+ } else {
+ assert(!_debugInitialized);
+ initInstances();
+ assert(_debugInitialized);
+
+ assert(!_debugServiceExtensionsRegistered);
+ initServiceExtensions();
+ assert(_debugServiceExtensionsRegistered);
+ }
developer.postEvent('Flutter.FrameworkInitialization', <String, String>{});
@@ -87,6 +95,10 @@ abstract class BindingBase {
/// different [Window] implementation, such as a [TestWindow].
ui.Window get window => ui.window;
+ @protected
+ void initBackgroundInstances() {
+ }
+
/// The initialization method. Subclasses override this method to hook into
/// the platform and otherwise configure their services. Subclasses must call
/// "super.initInstances()".
diff --git a/packages/flutter/lib/src/services/binding.dart b/packages/flutter/lib/src/services/binding.dart
index 227b9070d..cf23d931b 100644
--- a/packages/flutter/lib/src/services/binding.dart
+++ b/packages/flutter/lib/src/services/binding.dart
@@ -19,6 +19,16 @@ import 'system_channels.dart';
/// bundle, and implements the `ext.flutter.evict` service extension (see
/// [evict]).
mixin ServicesBinding on BindingBase {
+ @override
+ void initBackgroundInstances() {
+ super.initBackgroundInstances();
+ _instance = this;
+ _defaultBinaryMessenger = createBinaryMessenger();
+ window.onPlatformMessage = defaultBinaryMessenger.handlePlatformMessage;
+ initLicenses();
+ SystemChannels.system.setMessageHandler(handleSystemMessage);
+ }
+
@override
void initInstances() {
super.initInstances();
diff --git a/packages/flutter/lib/src/widgets/binding.dart b/packages/flutter/lib/src/widgets/binding.dart
index d511001d6..72356e771 100644
--- a/packages/flutter/lib/src/widgets/binding.dart
+++ b/packages/flutter/lib/src/widgets/binding.dart
@@ -250,6 +250,14 @@ abstract class WidgetsBindingObserver {
/// The glue between the widgets layer and the Flutter engine.
mixin WidgetsBinding on BindingBase, ServicesBinding, SchedulerBinding, GestureBinding, RendererBinding, SemanticsBinding {
+ @override
+ @protected
+ void initBackgroundInstances() {
+ super.initBackgroundInstances();
+
+ _backgroundInstance = this;
+ }
+
@override
void initInstances() {
super.initInstances();
@@ -352,6 +360,9 @@ mixin WidgetsBinding on BindingBase, ServicesBinding, SchedulerBinding, GestureB
));
}
+ static WidgetsBinding get backgroundInstance => _backgroundInstance;
+ static WidgetsBinding _backgroundInstance;
+
/// The current [WidgetsBinding], if one has been created.
///
/// If you need the binding to be constructed before calling [runApp],
@@ -1186,6 +1197,13 @@ class RenderObjectToWidgetElement<T extends RenderObject> extends RootRenderObje
///
/// This is the glue that binds the framework to the Flutter engine.
class WidgetsFlutterBinding extends BindingBase with GestureBinding, ServicesBinding, SchedulerBinding, PaintingBinding, SemanticsBinding, RendererBinding, WidgetsBinding {
+ WidgetsFlutterBinding({bool background,}) : super(background: background);
+
+ static WidgetsBinding ensureBackgroundInitialized() {
+ if (WidgetsBinding.backgroundInstance == null)
+ WidgetsFlutterBinding(background: true);
+ return WidgetsBinding.backgroundInstance;
+ }
/// Returns an instance of the [WidgetsBinding], creating and
/// initializing it if necessary. If one is created, it will be a
diff --git a/packages/flutter/lib/src/widgets/widget_inspector.dart b/packages/flutter/lib/src/widgets/widget_inspector.dart
index 4d021afd8..e0905d2e8 100644
--- a/packages/flutter/lib/src/widgets/widget_inspector.dart
+++ b/packages/flutter/lib/src/widgets/widget_inspector.dart
@@ -956,7 +956,7 @@ mixin WidgetInspectorService {
return true;
}());
- SchedulerBinding.instance.addPersistentFrameCallback(_onFrameStart);
+ SchedulerBinding.instance?.addPersistentFrameCallback(_onFrameStart);
final FlutterExceptionHandler structuredExceptionHandler = _reportError;
final FlutterExceptionHandler defaultExceptionHandler = FlutterError.onError;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment