MixinNetwork / flutter-plugins

🧱 Flutter plugins used in Mixin Messenger.
MIT License
442 stars 190 forks source link

Zone mismatch. desktop_webview_window 0.2.3 #327

Open huangsir0 opened 6 months ago

huangsir0 commented 6 months ago

A clear and concise description of what the bug is.

Reproduce Steps

 if (runWebViewTitleBarWidget(args)) {
    return;
  }

Error log

════════ Exception caught by Flutter framework ═════════════════════════════════
The following assertion was thrown during runApp:
Zone mismatch.
The Flutter bindings were initialized in a different zone than is now being used. This will likely cause confusion and bugs as any zone-specific configuration will inconsistently use the configuration of the original binding initialization zone or this zone based on hard-to-predict factors such as which zone was active when a particular callback was set.
It is important to use the same zone when calling `ensureInitialized` on the binding as when calling `runApp` later.
To make this warning fatal, set BindingBase.debugZoneErrorsAreFatal to true before the bindings are initialized (i.e. as the first statement in `void main() { }`).

When the exception was thrown, this was the stack:
#0      BindingBase.debugCheckZone.<anonymous closure> (package:flutter/src/foundation/binding.dart:495:29)
binding.dart:495
#1      BindingBase.debugCheckZone (package:flutter/src/foundation/binding.dart:500:6)
binding.dart:500
#2      runApp (package:flutter/src/widgets/binding.dart:1212:18)
binding.dart:1212
#3      runWebViewTitleBarWidget.<anonymous closure> (package:desktop_webview_window/src/title_bar.dart:34:7)
title_bar.dart:34
#8      runWebViewTitleBarWidget (package:desktop_webview_window/src/title_bar.dart:31:3)
title_bar.dart:31
#9      main (package:walang/main.dart:19:7)
main.dart:19
#10     _runMain.<anonymous closure> (dart:ui/hooks.dart:299:23)
hooks.dart:299
#11     _delayEntrypointInvocation.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:297:19)
isolate_patch.dart:297
#12     _RawReceivePort._handleMessage (dart:isolate-patch/isolate_patch.dart:184:12)
isolate_patch.dart:184
(elided 4 frames from dart:async)
════════════════════════════════════════════════════════════════════════════════
../../flutter/shell/platform/embedder/embedder.cc (2472): 'FlutterEngineSendPlatformMessage' returned 'kInvalidArguments'. Invalid engine handle.

**Version

LightningDev1 commented 5 months ago

I fixed this by moving WidgetsFlutterBinding.ensureInitialized() to after the runWebViewTitleBarWidget check.

The reason this happens is because runWebViewTitleBarWidget run the titlebar widget in a guarded zone, which will be different than the one the widget binding is initialized in, if you initialize it before running the titlebar widget.

bool runWebViewTitleBarWidget(
  List<String> args, {
  WidgetBuilder? builder,
  Color? backgroundColor,
  void Function(Object error, StackTrace stack)? onError,
}) {
  // ...
  runZonedGuarded( // <-- this is what causes the problem
    () {
      WidgetsFlutterBinding.ensureInitialized();
      runApp(_TitleBarApp(
        // ...
      ));
    },
    onError ??
        (e, s) {
          debugPrint('WebViewTitleBar: unhandled expections: $e, $s');
        },
  );

  return true;
}