Home > front end >  'canGoBack' method isn't defined for flutter WebViewPlus
'canGoBack' method isn't defined for flutter WebViewPlus

Time:12-07

I'm trying to use ( canGoBack() ) method with (flutter webview plus) plugin but it isn't work!!

vs code said:

The method 'canGoBack' isn't defined for the type 'WebViewPlusController'. Try correcting the name to the name of an existing method, or defining a method named 'canGoBack'.

this is my code:

//import 'dart:async';
import 'package:flutter/material.dart';
import 'package:webview_flutter_plus/webview_flutter_plus.dart';

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

  @override
  State<HomeScreen> createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
  late WebViewPlusController controller;
  @override
  Widget build(BuildContext context)  => WillPopScope (
    onWillPop: () async {
      if (await controller.canGoBack() ) {
      return false; }
    },
    child: SafeArea(
      child: Scaffold(
        // ignore: avoid_unnecessary_containers
        body: Container(
          child: WebViewPlus(
            initialUrl: 'assets/index.html',
            javascriptMode: JavascriptMode.unrestricted,
            onWebViewCreated: (controller) {
              this.controller = controller;
              },
            ),
          ),
        ),
      ),
    );
  }

how can I solve this problem?

I'm trying to use ( canGoBack() ) method with (flutter webview plus) plugin but it isn't work!!

CodePudding user response:

To use the method canGoBack you have to access the WebViewPlusController's property webViewController, here an example:

if (await controller.webViewController.canGoBack()) {
    return false;
}

More information in: https://pub.dev/documentation/webview_flutter_plus/latest/webview_flutter_plus/WebViewPlusController-class.html

  • Related