Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make my injected javascript code appear when theres a final Completer?

Hi Im new to flutter and this is the only remaining problem I have for my very first app to be finished, for some reason the injected javascript I used is not appearing in the WebView app.

FIXED (sorry for the delayed edit: the code was (await _controller.future).evaluateJavascript("document.getElementsByClassName('footer-container')[0].style.display='none';")

Note: Im using the webview_flutter by the flutter dev team and I want to have BOTH refresh FAB and injected code working at the same time

my code is below:

import 'dart:async';
import 'dart:math';
import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';

class HomePage extends StatefulWidget {
  HomePage({Key key}) : super(key: key);

  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {

  WebViewController _myController;
      final Completer<WebViewController> _controller =
      Completer<WebViewController>();
  @override
  Widget build(BuildContext context) {
    return SafeArea(
            child: Scaffold(
                  body: WebView(
                  initialUrl: 'https://syncshop.online/en/',
                  javascriptMode: JavascriptMode.unrestricted,
                  onWebViewCreated: (controller) {
                  _controller.complete(controller);
                },
          onPageFinished: (controller) {
          _myController.evaluateJavascript("document.getElementsByClassName('footer-container')[0].style.display='none';");
          },
          ),
    floatingActionButton: FutureBuilder<WebViewController>(
        future: _controller.future,
        builder: (BuildContext context, AsyncSnapshot<WebViewController> controller) {
          if (controller.hasData) {
            return FloatingActionButton(
            onPressed: () {
              controller.data.reload();
            },
            child: Icon(Icons.refresh),
          );
          }
          return Container();
        }),
          ),
      );
    }
}
like image 937
KDC Avatar asked Apr 20 '26 08:04

KDC


1 Answers

I think, _myController is not assigned anything.

So, Instead try using the _controller for evaluateJavascript(). Like this:

(await _controller.future).evaluateJavascript("document.getElementsByClassName('footer-container')[0].style.display='none';");
like image 127
Harshvardhan Joshi Avatar answered Apr 21 '26 22:04

Harshvardhan Joshi