-1

Im calling an html page in flutter using flutter_inappwebview package. Now i have create a function in javascript and i want to pass data in the function from flutter.I want solution that will work on every platform.

So i have done it like :

await webViewController?.evaluateJavascript(source: """ (function() {

    if (window.receivecmpdata) {
      window.receivecmpdata('$data');
      console.log('Sent to JavaScript: $data');
    } else {
      console.log('receivecmpdata function is not defined');
    }
  })();
""");

It is working on windows but in web its not working.

SO i have treied passing the data in the cookies : await cookieManager.setCookie( url: WebUri("http://192.168.29.120"), // Use your domain here name: "billdata", value: "your_value_here", expiresDate: DateTime.now().add(Duration(days: 30)).millisecondsSinceEpoch, isHttpOnly: false, isSecure: false, // Set true for HTTPS only );

But this give error like

  1. SecurityError: Failed to read a named property 'console' from 'Window': Blocked a frame with origin "http://localhost:63961" from accessing a cross-origin frame.

2)SecurityError: Failed to read a named property 'history' from 'Window': Blocked a frame with origin "http://localhost:63961" from accessing a cross-origin frame.

3)Access to XMLHttpRequest at 'http://gc.kis.v2.scr.kaspersky-labs.com/7D8B79A2-8974-4D7B-A76A-F4F29624C06BSRPpuIkUKS8xuha8q3mQnZS5cgyjZ2cMLdA80akhF7KaSUR6T1U2ki3C93iAgkl_y_K6-wIjBC_fmRK3zpdvmQ/init?data=eyJ1cmwiOiJodHRwOi8vMTkyLjE2OC4yOS4xMjAvcnB0Q2hhbGxhblByaW50L3JwdENoYWxsYW5QcmludC5odG1sIiwicGx1Z2lucyI6IndzbSZ2cyZ3bnQmdWEmY2EmY2ImeGhyX2NvbnRlbnQiLCJkYXRhIjp7ImRhdGEiOlt7InBsdWdpbiI6IndzbSIsInBhcmFtZXRlcnMiOiJ7XCJyZWZlcnJlclwiOlwiaHR0cDovL2xvY2FsaG9zdDo2Mzk2MS9cIixcInN0dWJJZFwiOlwiXCJ9In0seyJwbHVnaW4iOiJ3bnQiLCJwYXJhbWV0ZXJzIjoie1wicmVmZXJyZXJcIjpcImh0dHA6Ly9sb2NhbGhvc3Q6NjM5NjEvXCJ9In0seyJwbHVnaW4iOiJ4aHJfY29udGVudCIsInBhcmFtZXRlcnMiOiJ7XCJyZWZlcnJlclwiOlwiaHR0cDovL2xvY2FsaG9zdDo2Mzk2MS9cIn0ifV19LCJpc1RvcExldmVsIjpmYWxzZSwicGFnZVN0YXJ0VGltZSI6MCwibmF2aWdhdGlvblN0YXJ0VGltZSI6MTc0MDY1MjEwMDg5OX0%3D&nocache=10c00&tm=2025-02-27T10%3A28%3A22.707Z' from origin 'null' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

2 Answers 2

0

You can fix on client-side flutter:

1- First step, make set options put InAppWebView:

  InAppWebViewGroupOptions options = InAppWebViewGroupOptions(
    crossPlatform: InAppWebViewOptions(
      javaScriptEnabled: true, // Make sure JavaScript is enabled
      mediaPlaybackRequiresUserGesture: false, // For media (if needed)
    ),
    ios: IOSInAppWebViewOptions(
      allowsInlineMediaPlayback: true,  // Required for iOS media
    ),

2- Step Two:

await webViewController?.evaluateJavascript(source: """
window.postMessage('$data', '*');
console.log('Sent to JavaScript: $data');
""");
Sign up to request clarification or add additional context in comments.

Comments

0

initialSettings: InAppWebViewSettings( allowContentAccess: true, cacheEnabled: true, domStorageEnabled: true, saveFormData: true, allowsInlineMediaPlayback: true, allowFileAccess: true, isFindInteractionEnabled: true, safeBrowsingEnabled: false, javaScriptCanOpenWindowsAutomatically:true , iframeSandbox:{Sandbox.ALLOW_SCRIPTS}, disableDefaultErrorPage: true, javaScriptEnabled: true, allowFileAccessFromFileURLs: true, allowUniversalAccessFromFileURLs: true, ), InAppWebViewGroupOptions is deprecated im using latest version

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.