flutter-plugins icon indicating copy to clipboard operation
flutter-plugins copied to clipboard

Zone mismatch. desktop_webview_window 0.2.3

Open huangsir0 opened this issue 1 year ago • 4 comments

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

  • Flutter Version: 3.19.4
  • OS: Windows
  • plugin: desktop_webview_window 0.2.3

huangsir0 avatar Apr 18 '24 06:04 huangsir0

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;
}

LightningDev1 avatar May 07 '24 12:05 LightningDev1

@LightningDev1 Can you show your code? I'm having the same issue and I applied your suggestion but it didn't work. This is my code:

void main(List<String> args) async {
  // Add this check for webview window
  if (runWebViewTitleBarWidget(args)) {
    WidgetsFlutterBinding.ensureInitialized();
    return;
  }

  // Rest of your main function...
  runApp(const MyApp());
}

My Flutter version:

[✓] Flutter (Channel stable, 3.27.0, on macOS 15.1.1 24B91 darwin-arm64, locale en-VN)

trunghq3101 avatar Feb 07 '25 05:02 trunghq3101

void main(List<String> args) async {
  // Add this check for webview window
  if (runWebViewTitleBarWidget(args)) {
    return;
  }

  // Rest of your main function...
  WidgetsFlutterBinding.ensureInitialized();
  runApp(const MyApp());
}

This is what I meant. You shouldn't initialize Flutter as the WebView titlebar already does it.

LightningDev1 avatar Feb 07 '25 05:02 LightningDev1

@LightningDev1 Oh it's working! I tried your code before but I probably didn't stop and rebuild the app. It does the trick. Thank you!

trunghq3101 avatar Feb 07 '25 07:02 trunghq3101