You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter WebViewPlus插件canGoBack方法未定义问题求助

解决WebViewPlusController中canGoBack方法未定义的问题

VS Code提示错误:

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'.

问题原因

WebViewPlusController本身没有直接提供canGoBack()方法,但它内部封装了原生的WebViewController实例,后者支持该方法调用。

解决方案

将代码里的controller.canGoBack()替换为controller.webViewController.canGoBack()即可。同时建议补充goBack()调用,确保后退逻辑完整执行。

修改后的完整代码

//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.webViewController.canGoBack()) {
        controller.webViewController.goBack();
        return false;
      }
      return true;
    },
    child: SafeArea(
      child: Scaffold(
        body: WebViewPlus(
          initialUrl: 'assets/index.html',
          javascriptMode: JavascriptMode.unrestricted,
          onWebViewCreated: (controller) {
            this.controller = controller;
          },
        ),
      ),
    ),
  );
}

内容的提问来源于stack exchange,提问作者Abdulhamid Mohamed

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 02:55:43