Search code examples
flutterandroid-studiowebviewflutter-dependenciesmobile-application

The method 'WebView' isn't defined for the type 'MyApp"?


I'm facing the problem with WebView for flutter, code editor is not recognizing the WebView it says The method 'WebView' isn't defined for the type 'MyApp'. (Documentation) Try correcting the name to the name of an existing method, or defining a method named 'WebView'.

I have added WebView package which is "webview_flutter" by this command $ flutter pub add webview_flutter,

Flutter doctor:

D:\src\flutter\bin\flutter.bat doctor --verbose
[√] Flutter (Channel stable, 3.19.0, on Microsoft Windows [Version 10.0.19045.4046], locale en-US)
    • Flutter version 3.19.0 on channel stable at D:\src\flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision bae5e49bc2 (5 days ago), 2024-02-13 17:46:18 -0800
    • Engine revision 04817c99c9
    • Dart version 3.3.0
    • DevTools version 2.31.1

[√] Windows Version (Installed version of Windows is version 10 or higher)

[√] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
    • Android SDK at C:\Users\dell\AppData\Local\Android\sdk
    • Platform android-34, build-tools 34.0.0
    • Java binary at: D:\Android Sudio Setup\Installed\jbr\bin\java
    • Java version OpenJDK Runtime Environment (build 17.0.7+0-b2043.56-10550314)
    • All Android licenses accepted.

[√] Chrome - develop for the web
    • Chrome at C:\Program Files\Google\Chrome\Application\chrome.exe

[X] Visual Studio - develop Windows apps
    X Visual Studio not installed; this is necessary to develop Windows apps.
      Download at https://visualstudio.microsoft.com/downloads/.
      Please install the "Desktop development with C++" workload, including all of its default components

[√] Android Studio (version 2023.1)
    • Android Studio at D:\Android Sudio Setup\Installed
    • Flutter plugin can be installed from:
       https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
       https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 17.0.7+0-b2043.56-10550314)

[√] VS Code (version 1.86.2)
    • VS Code at C:\Users\dell\AppData\Local\Programs\Microsoft VS Code
    • Flutter extension version 3.82.0

[√] Connected device (3 available)
    • Windows (desktop) • windows • windows-x64    • Microsoft Windows [Version 10.0.19045.4046]
    • Chrome (web)      • chrome  • web-javascript • Google Chrome 121.0.6167.185
    • Edge (web)        • edge    • web-javascript • Microsoft Edge 121.0.2277.112
    ! Device ZXPRHAJZWSEM9LNN is not authorized.
      You might need to check your device for an authorization dialog.

[√] Network resources
    • All expected network resources are available.

! Doctor found issues in 1 category.
Process finished with exit code 0

pubspec.yaml codes also have problem with webview_flutter: ^4.7.0 dependency it underline the word webview and says: Typo: In word 'webview'.

I have done almost everything to fix this error/problem but I think it will never go away from computer but I hope I will be get rid of this error with your help.

Here is complete porject link. Drive:

https://drive.google.com/file/d/1IfRPvrw1fseKM-qrM_bowYyoAU-h5FQM/view?usp=sharing


Solution

  • Starting from version 4.0.0, the WebView widget is removed and its functionality has been split into WebViewController and WebViewWidget. If you had the old implementation of WebView in your codebase, follow this migration guide.

    Generally, you should now define a controller like this:

    controller = WebViewController()
      ..setJavaScriptMode(JavaScriptMode.unrestricted)
      ..setBackgroundColor(const Color(0x00000000))
      ..setNavigationDelegate(
        NavigationDelegate(
          onProgress: (int progress) {
            // Update loading bar.
          },
          onPageStarted: (String url) {},
          onPageFinished: (String url) {},
          onWebResourceError: (WebResourceError error) {},
          onNavigationRequest: (NavigationRequest request) {
            if (request.url.startsWith('https://www.youtube.com/')) {
              return NavigationDecision.prevent;
            }
            return NavigationDecision.navigate;
          },
        ),
      )
      ..loadRequest(Uri.parse('https://flutter.dev'));
    

    Pass the controller to the WebViewWidget:

    @override
    Widget build(BuildContext context) {
      return Scaffold(
        appBar: AppBar(title: const Text('Flutter Simple Example')),
        body: WebViewWidget(controller: controller),
      );
    }
    

    References: