skip to Main Content

I’m following this tutorial, and once I run flutter run on project it returns these error messages,

I have tried upgrading flutter SDK and changing Android minSDK version and nothing seems to work.

lib/main.dart:25:7: Error: The getter 'WebView' isn't defined for the class '_WebViewAppState'.
 - '_WebViewAppState' is from 'package:webview_in_flutter/main.dart' ('lib/main.dart').
Try correcting the name to the name of an existing getter, or defining a getter or field named 'WebView'.
      WebView.platform = SurfaceAndroidWebView();
      ^^^^^^^
lib/main.dart:25:26: Error: The method 'SurfaceAndroidWebView' isn't defined for the class '_WebViewAppState'.
 - '_WebViewAppState' is from 'package:webview_in_flutter/main.dart' ('lib/main.dart').
Try correcting the name to the name of an existing method, or defining a method named 'SurfaceAndroidWebView'.
      WebView.platform = SurfaceAndroidWebView();
                         ^^^^^^^^^^^^^^^^^^^^^
lib/main.dart:37:19: Error: Couldn't find constructor 'WebView'.
      body: const WebView(
                  ^^^^^^^

What’s wrong here, this happens for both Android and Web browser debugging.

2

Answers


  1. You can get controller from ‘onWebViewCreated’

    class WebViewPage extends StatelessWidget {
       const WebViewPage():super(key: key);
    
       late WebViewController webController;
       
       @override
      Widget build(BuildContext context) {
        return Scaffold(
        ...
        WebView(
           onWebViewCreated: (WebViewController c) {
              webController = c;
              // use stateful widget and setState here.
           },
           initialUrl: 'your_url',
           // optional (as per you requirement)
           javascriptMode: JavascriptMode.unrestricted,
         ),
        ...
        );
    }
    
    Login or Signup to reply.
  2. found solution on: https://github.com/flutter/flutter/issues/117808

    class _WebViewAppState extends State<WebViewApp> {
              @override
              final 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'));
        
          Widget build(BuildContext context) {
            return Scaffold(
              appBar: AppBar(
                title: const Text('Flutter WebView'),
              ),
              body: WebViewWidget(controller: controller),
            );
          }
        }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search