init static method
Initialize the shared context.
If context is provided, the current FlutterView is obtained via
View.of(context) and MediaQueryData is derived from it.
If context is omitted, the method attempts lazy initialization using the
first available view from WidgetsBinding.instance.platformDispatcher.views.
This requires the Flutter binding to be ready (i.e., after runApp or
WidgetsFlutterBinding.ensureInitialized() in tests).
Implementation
static void init([BuildContext? context]) {
final binding = WidgetsBinding.instance;
final FlutterView view;
if (context != null) {
view = View.of(context);
} else {
final views = binding.platformDispatcher.views;
if (views.isEmpty) {
throw StateError(
'FlutterSExtensions initialization failed: No FlutterView available. '
'Provide a BuildContext to FlutterSExtensions.init(context).',
);
}
view = views.first;
}
_view = view;
_media = MediaQueryData.fromView(view);
_initialized = true;
if (!_observerRegistered) {
binding.addObserver(_singleton);
_observerRegistered = true;
}
}