Home > OS >  WebView - problem with "complete(controller)" in "onWebViewCreated"
WebView - problem with "complete(controller)" in "onWebViewCreated"

Time:09-01

I'm new in flutter and I was following YT in this specific case. On this tut everything is working but on my side, I have an error in this line below.

onWebViewCreated: (controller) => controller.complete(controller),

I receive this message: "The method 'complete' isn't defined for the type 'WebViewController'. Try correcting the name to the name of an existing method, or defining a method named 'complete'."

Can someone help me with figuring out how to fix that?

Full code below:

import 'dart:async';

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

void main() {
  runApp(
    MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const HomePage(),
    ),
  );
}

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

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  final controller = Completer<WebViewController>();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Home Page'),
      ),
      body: WebView(
        initialUrl: "https://google.com",
        onWebViewCreated: (controller) => controller.complete(controller),
      ),
      bottomNavigationBar: Container(
        color: Colors.blue,
        child: Padding(
          padding: const EdgeInsets.only(bottom: 5, right: 20),
          child: ButtonBar(
            children: [
              navigationButton(
                  Icons.chevron_left, (controller) => goBack(controller)),
              navigationButton(
                  Icons.chevron_right, (controller) => goForward(controller)),
            ],
          ),
        ),
      ),
    );
  }

  Widget navigationButton(
      IconData icon, Function(WebViewController) onPressed) {
    return FutureBuilder(
      future: controller.future,
      builder: (context, AsyncSnapshot snapshot) {
        if (snapshot.hasData) {
          return IconButton(
              icon: Icon(
                icon,
                color: Colors.white,
              ),
              onPressed: () => onPressed(snapshot.data));
        } else {
          return Container(
            height: 50,
          );
        }
      },
    );
  }

  void goBack(WebViewController controller) async {
    final canGoBack = await controller.canGoBack();

    if (canGoBack) {
      controller.goBack();
    }
  }

  void goForward(WebViewController controller) async {
    final canGoForward = await controller.canGoForward();

    if (canGoForward) {
      controller.goForward();
    }
  }
}

Also, here you can see what the app looks like. I draw two red arrows because there should be two white arrows that are coded but they are not in the app. App view

CodePudding user response:

You are using same name in onWebViewCreated and compiler cannot separate theme from each other, change to this:

onWebViewCreated: (WebViewController webViewController) {
    controller.complete(webViewController);
},
  • Related