-1
await webViewController.evaluateJavascript(source: """
    \$(document).bind('DOMSubtreeModified', function () {
      if(\$("a[href^='/GenericUpload/GetFile']").length > 0){

    \$("a[href^='/GenericUpload/GetFile']").off('click').on('click', function (){ window.flutter_inappwebview.callHandler('MobileAppDownload',\$(this).attr('href')); });
      }}); """);

This code find a button in my app and execute my action. this code work once and after that not working. MUST uninstall app or clear data and cache in android to work again. this code work in IOS normally.

2
  • 2
    Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Commented Oct 23, 2024 at 20:17
  • Not sure if this is your problem, but DOMSubtreeModified has been deprecated long ago. Commented Oct 24, 2024 at 8:52

1 Answer 1

-1

You can use MutationObserver to Observe changes only within the specific container or element where the button is expected to appear. This reduces the number of observed nodes and improves performance

import 'package:flutter/material.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key});

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  InAppWebViewController? _webViewController;

  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    // Clean up resources (e.g., disconnect observers)
    if (_webViewController != null) {
      _webViewController!.clearCache(); 
    }
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: InAppWebView(
        initialUrlRequest: URLRequest(url: Uri.parse('your_web_page_url')),
        onWebViewCreated: (controller) {
          _webViewController = controller;
        },
        onPageFinished: (controller, url) async {
          await controller.evaluateJavascript(source: """
            const targetNode = document.querySelector('#your-target-container'); // Replace with the actual container ID
            const config = { childList: true, subtree: true };

            const observer = new MutationObserver(mutations => {
              mutations.forEach(mutation => {
                if (mutation.type === 'childList') {
                  const button = document.querySelector("a[href^='/GenericUpload/GetFile']");
                  if (button) {
                    button.addEventListener('click', () => {
                      window.flutter_inappwebview.callHandler('MobileAppDownload', button.getAttribute('href'));
                    });
                    observer.disconnect(); // Stop observing after the button is found
                  }
                }
              });
            });

            if (targetNode) {
              observer.observe(targetNode, config);
            }
          """);
        },
      ),
    );
  }
}
Sign up to request clarification or add additional context in comments.

Comments

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.