Get Json response when Webview redirect to another page in Flutter

2,894

I was not aware that I found the solution because the html content was very complex. By using webview_flutter plugin problem is sorted. Build your widget as it is;

WebView(
    initialUrl: new Uri.dataFromString(widget.d3sc, mimeType: 'text/html').toString(),

    onWebViewCreated: (controller) {
      _controller = controller;
    },
    javascriptMode: JavascriptMode.unrestricted,
    gestureNavigationEnabled: true,
    onPageFinished: (_) {
      readResponse();
    },
  )

readResponse function is called whenever navigation occurs and page opening finished. We can get page's html content as string by using following function;

    void readResponse() async
  {
  

    setState(() {
      _controller.evaluateJavascript("document.documentElement.innerHTML").then((value) async {

        if(value.contains("name=\"paymentId\"")){

// value contains the html data of page as string
 ...

I could not convert html data to json map but we can reach whatever we want in this string by simply using substring method.

Note: webview_flutter version ^1.0.7 is used for solution in this case.

Share:
2,894
Muhtar
Author by

Muhtar

Updated on December 29, 2022

Comments

  • Muhtar
    Muhtar 10 months

    flutter_webview_plugin^0.3.11 I am building a payment gateway and in 3d secure mode I need to use Webview. Launch Webview with a response in type of html string.

       return Scaffold(
      body: WebviewScaffold(
        withJavascript: true,
        key: _scaffoldKey,
        url: new Uri.dataFromString(widget.d3sc, mimeType: 'text/html').toString(),
      ),
    );
    

    It prints on the screen the security code phase. When I submit the code it redirects automatically to callback url and I can not manage to connect or get response from this url. I need to get response when code is submitted and page is redirected for having token for closing 3d secure payment. Any idea or solution ?

    Tried below code. Although it prints the redirected url, there is no json response in response element.

     webView.launch(new Uri.dataFromString(widget.d3sc, mimeType: 'text/html').toString() ,withJavascript: true, withLocalStorage: true);
      webView.show();
    
      webView.onUrlChanged.listen((String event) async {
        print("${event} even eevent");
        setState(() {
          webView
              .evalJavascript("document.documentElement.innerText")
              .then((response) => print("$response response response "));
    
        });
      });