Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

flutter_inappwebview.callHandler is not a function #230

Closed
koteezy opened this issue Dec 17, 2019 · 5 comments
Closed

flutter_inappwebview.callHandler is not a function #230

koteezy opened this issue Dec 17, 2019 · 5 comments

Comments

@koteezy
Copy link

koteezy commented Dec 17, 2019

Environment

Flutter version: 1.12.13+hotfix.5
Plugin version: ^2.0.0
Android version: 8.1.0
Device information: Honor 7A ( DUA-L22 )

Description

Hello!

On my Nexus 5, everything works fine, but, on that honor, i can't send message from JS, because of this error. In console ( not chrome ), i have no errors, no warnings, nothing, so.. i don't event have an idea what's going wrong.

Images

Errors

Flutter doctor

[✓] Flutter (Channel stable, v1.12.13+hotfix.5, on Mac OS X 10.15.1 19B88, locale ru-RU)
   • Flutter version 1.12.13+hotfix.5 at /Users/uebanchik/development/flutter
   • Framework revision 27321ebbad (6 days ago), 2019-12-10 18:15:01 -0800
   • Engine revision 2994f7e1e6
   • Dart version 2.7.0


[✓] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
   • Android SDK at /Users/uebanchik/Library/Android/sdk
   • Android NDK location not configured (optional; useful for native profiling support)
   • Platform android-29, build-tools 29.0.2
   • Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java
   • Java version OpenJDK Runtime Environment (build 1.8.0_202-release-1483-b49-5587405)
   • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 11.2.1)
   • Xcode at /Applications/Xcode.app/Contents/Developer
   • Xcode 11.2.1, Build version 11B500
   • CocoaPods version 1.8.4

[✓] Android Studio (version 3.5)
   • Android Studio at /Applications/Android Studio.app/Contents
   • Flutter plugin version 42.0.1
   • Dart plugin version 191.8593
   • Java version OpenJDK Runtime Environment (build 1.8.0_202-release-1483-b49-5587405)

[✓] VS Code (version 1.41.0)
   • VS Code at /Applications/Visual Studio Code.app/Contents
   • Flutter extension version 3.7.1

[✓] Connected device (1 available)
   • DUA L22 • MNV9K18C21404510 • android-arm64 • Android 8.1.0 (API 27)

• No issues found!

@pichillilorenzo
Copy link
Owner

Please don't post the same issue as #218. I will close this issue. You can continue the discussion on #218, thanks!

However window.flutter_inappwebview._callHandler should not be used because it's a private API that I use to manage javascript handlers. You should use only window.flutter_inappwebview.callHandler.

Also, did you put your code inside the flutterInAppWebViewPlatformReady event callback? You need to wait that event to be sure window.flutter_inappwebview.callHandler is available, like this:

<script>
   window.addEventListener("flutterInAppWebViewPlatformReady", function(event) {
     window.flutter_inappwebview.callHandler('handlerFoo').then(function(result) {
       console.log(result);
     });

     window.flutter_inappwebview.callHandler('handlerFooWithArgs', 1, true, ['bar', 5], {foo: 'baz'}).then(function(result) {
       console.log(result);
     });
   });
</script>

If you can, post your code (dart and html code) that is not working in order to debug and test it.

This was referenced Jul 6, 2020
@msutharworkspace
Copy link

class _MyAppState extends State<MyApp> {
  InAppWebViewController? _webViewController;
  final _settings = InAppWebViewSettings(
      useShouldOverrideUrlLoading: true,
      mediaPlaybackRequiresUserGesture: false,
      allowsInlineMediaPlayback: true,
      useHybridComposition: true,
      clearCache: true,
      iframeAllow: "camera; microphone",
      iframeAllowFullscreen: true);
  //window.flutter_inappwebview.callHandler('ready', {"name":"hello"});

  String initialData = """
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    </head>
    <body>
        <h1>JavaScript Handlers (Channels) TEST</h1>
        <script>
           console.log("script=====");
           console.log(window)
           window.flutter_inappwebview.callHandler('ready', {"name":"hello"});
           if(window.flutter_inappwebview)
           {
            console.log("not undefined");
           }
          
            window.addEventListener("flutterInAppWebViewPlatformReady", function(event) {
             console.log("flutterInAppWebViewPlatformReady");
                window.flutter_inappwebview._callHandler('handlerFoo')
                  .then(function(result) {
                    // print to the console the data coming
                    // from the Flutter side.
                    console.log(JSON.stringify(result));
                    
                    window.flutter_inappwebview
                      .callHandler('handlerFooWithArgs', 1, true, ['bar', 5], {foo: 'baz'}, result);
                });
            });
        </script>
    </body>
</html>
                      """;
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('InAppWebView Example'),
        ),
        body: Container(
            child: Column(children: <Widget>[
          Expanded(
            child: InAppWebView(
              initialData: InAppWebViewInitialData(data: initialData),
              initialSettings: _settings,
              onPermissionRequest: (controller, request) async {
                return PermissionResponse(
                    resources: request.resources,
                    action: PermissionResponseAction.GRANT);
              },
              onWebViewCreated: (InAppWebViewController controller) {
                print("onWebViewCreated=====");
                _webViewController = controller;

                // _webViewController!.addJavaScriptHandler(
                //     handlerName: 'handlerFoo',
                //     callback: (args) {
                //       // return data to JavaScript side!
                //       return {'bar': 'bar_value', 'baz': 'baz_value'};
                //     });
                //
                // _webViewController!.addJavaScriptHandler(
                //     handlerName: 'handlerFooWithArgs',
                //     callback: (args) {
                //       print(args);
                //       // it will print: [1, true, [bar, 5], {foo: baz}, {bar: bar_value, baz: baz_value}]
                //     });
              },
              onLoadStop:
                  (InAppWebViewController controller, WebUri? url) async {
                print("onLoadStop=====");
                setState(() {});
              },
              onConsoleMessage: (controller, consoleMessage) {
                print(consoleMessage);
                // it will print: {message: {"bar":"bar_value","baz":"baz_value"}, messageLevel: 1}
              },
            ),
          ),
        ])),
      ),
    );
  }
}

@msutharworkspace
Copy link

Screenshot 2023-03-09 at 9 58 29 PM

@msutharworkspace
Copy link

Getting above error

Copy link

github-actions bot commented Oct 8, 2024

This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug and a minimal reproduction of the issue.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 8, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants